Combo box control--CComboBox

Source: Internet
Author: User

combo box control --CComboBox

The combo box is actually a combination of an edit box and a list box, divided into three types: simple combo box, drop-down (Dropdown) combo box and drop-down list combo box. The Properties panel sets the Type property. When dynamically created, BOOL create (DWORD dwstyle, const rect& RECT, cwnd* pParentWnd, UINT NID); Add the Cbs_dropdownlist parameter to the dwstyle. The display results are:

      

Common notification messages

When the combo box is manipulated, a notification message is sent to the parent window, and the notification messages and their meanings are as follows:

Cbn_closeup: Combo box list box component is closed, simple combo box does not send the notification message
CBN_DBLCLK: User double-clicks on a list item, only the simple combo box will send the notification message
Cbn_dropdown: Combo box list box component drop-down, simple combo box does not send the notification message
Cbn_editupdate: The message is sent when the edit box is ready to display the changed body, and the drop-down list combo box does not send the message
Cbn_editchange: The content of the edit box is changed by the user, and unlike cbn_editupdate, the message is emitted after the body of the edit box is refreshed, and the drop-down list combo box does not send the message
Cbn_errspace: Combo box cannot request enough memory to hold list items
Cbn_selendcancel: Indicates that the user's selection should be canceled, which causes the message to be sent when the user selects an item in the list box and then clicks the mouse outside the combo box control.
Cbn_selendok: The user selects an item and then presses the ENTER key or clicks the scroll down arrow, which indicates that the user has confirmed his or her choice
Cbn_killfocus: Combo box loses input focus
Cbn_selchange: The user changes the selection of the list by clicking or moving the arrow keys
Cbn_setfocus: Combo box gets input focus

Its message map is as follows:

On_cbn_selchange (Idc_combo_printers, &cprintdlgappdlg::oncbnselchangecomboprinters)

On_cbn_editchange (Idc_combo_printers,&cprintdlgappdlg::oncbneditchangecomboprinters)

On_cbn_closeup (Idc_combo_printers,&cprintdlgappdlg::oncbncloseupcomboprinters)

On_cbn_dropdown (Idc_combo_printers,&cprintdlgappdlg::oncbndropdowncomboprinters)

On_cbn_editupdate (Idc_combo_printers,&cprintdlgappdlg::oncbneditupdatecomboprinters)

ON_CBN_DBLCLK (Idc_combo_printers, &cprintdlgappdlg::oncbndblclkcomboprinters)

On_cbn_errspace (Idc_combo_printers,&cprintdlgappdlg::oncbnerrspacecomboprinters)

On_cbn_selendcancel (Idc_combo_printers,&cprintdlgappdlg::oncbnselendcancelcomboprinters)

On_cbn_selendok (Idc_combo_printers,&cprintdlgappdlg::oncbnselendokcomboprinters)

On_cbn_setfocus (Idc_combo_printers,&cprintdlgappdlg::oncbnsetfocuscomboprinters)

On_notify (nm_themechanged, Idc_combo_printers,&cprintdlgappdlg::onnmthemechangedcomboprinters)

Creating a combo box control

MFC encapsulates all the operations of a combo box control into the CComboBox class.

When you join a combo box in a dialog box, you can drag the ComboBox control into the dialog box template, and then configure various properties in the Properties panel. If created dynamically in the program, use the member function of the CComboBox class create. The prototype of the CREATE function is as follows:

Virtualbool Create (
DWORD Dwstyle,
Const rect& RECT,
cwnd* pParentWnd,
UINT NID
);

Where dwstyle specifies the style of the combo box control, Rect is the position and dimension of the combo box when the list box pops up, pParentWnd is a pointer to the parent window, and the ID of the combo box control cannot be specified for Null,nid. The focus is on the dwstyle parameter, which can specify the style of the combo box control, including the following:

Cbs_autohscroll: Style that makes the edit box component scroll horizontally
Cbs_disablenoscroll: Causes the list box to display a forbidden vertical scrollbar when scrolling is not required
Cbs_dropdown: Specify a drop-down combo box
Cbs_dropdownlist: Specify a drop-down list combo box
Cbs_hasstrings: Specifies a self-drawn combo box with a string
Cbs_lowercase: Automatically converts all text in the Edit box and list box to lowercase characters
Cbs_nointegralheight: The size of a combo box is specified by the application instead of Windows, and typically the size specified by Windows hides parts of the list item.
Cbs_oemconvert: Enables the body of the edit box component to convert from one to the other between the ANSI character set and the OEM character set. This is useful when you include a file name in the edit box
Cbs_ownerdrawfixed: Specifies a custom combo box, that is, the parent window is responsible for drawing the contents of the list box, and the list items have the same height
Cbs_ownerdrawvariable: Specifies a custom combo box, and the list item has a different height
Cbs_siimple: Specify a simple combo box
Cbs_sort: Automatically sort items in a list box component
Cbs_uppercase: Automatically converts all text in the Edit box and list box to uppercase characters

The dwstyle parameter can be a combination of the above styles. As with other controls, styles such as Ws_child, Ws_visible, Ws_tabstop, and Ws_vscroll are also typically specified when created.

When you add a combo box control directly in a dialog box template, the properties in its property page contain the above style, such as the property uppercase is set to True to specify the Cbs_uppercase style.

CComboBox the primary member function of the class

because combo boxes are composed of edit boxes and list boxes, there are many similarities between the action of a combo box and the action of the edit box and the list box. The member functions of the CComboBox class also have many similarities with the CEdit class and the member functions of the CListBox class, not only functionally similar, but even the function names and parameters are similar. The following is the main member function of the CComboBox class, which can be found in more detail in MSDN.

int GetCount () const;
Gets the number of list items in the list box for the combo box control.

int GetCurSel () const;
Gets the index of the selected item in the ListBox control's list box, or returns CB_ERR if no item is selected.

int SetCurSel (int nselect);
Select an item in the list box of the combo box control. The Nselect parameter specifies the index of the list item to select, and if 1 the current selection in the list box is unchecked and the edit box is emptied.

DWORD Geteditsel () const;
Gets the position of the start and end characters of the current selection in the edit box of the combo box control. The function returns a 32-digit number, a low 16-bit starting position, and a high 16-bit position of the first non-selectable character after the selection range. If the function is used in a drop-down list combo box, Cb_err is returned.

BOOL seteditsel (int nstartchar,intnendchar);
Used to select characters in the edit box of a combo box control. The nStartChar parameter specifies the starting position, and the nEndChar parameter specifies the terminating position.

dword_ptrgetitemdata (int nIndex) const;
Gets the 32-bit data that is associated with the specified item in the combo box. The nindex parameter specifies the index of an item of the combo box control's list box (starting at 0).

intsetitemdata (int nindex,dword_ptr dwitemdata);
Sets an associated 32-digit number for a specified combo box list item. The nindex parameter specifies the index of the list item to be set. The Dwitemdata parameter specifies the new value to associate.

voidgetlbtext (int nindex,cstring& rString) const;
Gets the string of an item from the list box of a combo box control. The nindex parameter specifies the index of the list item to get the string, and the CString parameter is used to receive the fetched string.

int getlbtextlen (int nIndex) const;
Gets the string length of an item in the list box of a combo box control. The nindex parameter specifies the index of the list item to get the length of the string.

int gettopindex () const;
Gets the index of the first visible item in the list box of a combo box control.

int settopindex (int nIndex);
Sets a specified item in the list box of a combo box control to be visible. The nindex parameter specifies the index of the list item. The function returns 0 if it succeeds, and returns CB_ERR if an error occurs.

boollimittext (int nmaxchars);
Used to limit the maximum length of bytes that a user can enter in the edit box of a combo box control. The Nmaxchars parameter specifies the maximum byte length to which the user can enter text, and if 0, the length is limited to 65,535 bytes.

intaddstring (LPCTSTR lpszstring);
Adds a new list item to the list box in the combo box control. The lpszstring parameter is a pointer to the string to be added. If the return value of the function is greater than or equal to 0, then it is the index of the new list item and returns CB_ERR if there is an error, and returns Cb_errspace if there is not enough memory to hold the new string.

intdeletestring (UINT nIndex);
Deletes a list item at a specified position in the combo box. The nindex parameter specifies the index of the list item to be deleted. If the return value of the function is greater than or equal to 0, then it is the number of remaining list items in the combo box. Returns CB_ERR if the index specified by nindex exceeds the number of list items.

intfindstring (int nstartafter,lpctstr lpszstring) const;
Finds but does not select the first list item that contains the specified prefix in the combo box control's list box. The Nstartafter parameter specifies the index of the list item before the first list item to be looked up. Lpszstring points to the string that contains the prefix to find. If the return value of the function is greater than or equal to 0, then it is the index of the matching list item and returns CB_ERR if the lookup fails.

int insertstring (int nindex,lpctstr lpszstring);
Inserts a list item into the list box of a combo box control. The nindex parameter specifies where to insert the list item, and the lpszstring parameter specifies the string to insert. The function returns the position where the string was inserted, and returns CB_ERR if there is an error, and returns Cb_errspace if there is not enough memory to hold the new string.

int selectstring (int nstartafter,lpctstr lpszstring);
Finds a string in the combo box control's list box, selects it if found, and displays it in the edit box. Parameters are the same as findstring. Returns the index of this list item if the string is found, and returns CB_ERR if the lookup fails, and the current selection does not change.

In addition, the CComboBox class inherits the member functions of the CWnd class GetWindowText, SetWindowText, and so on.

    typically when you use this control, you need to add the appropriate information when initializing        :for (int0; i < printers.size (); i++ )        {            m_comboprinters.addstring (printers.at (i));        }        M_comboprinters.setcursel (0);    When you need to clear all the information        , use while 0)        {            m_comboprinters.deletestring ( 0 );        }

Resources

Http://www.jizhuomi.com/software/189.html

Combo box control--CComboBox

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.