Add content
Combobox_addstring (hwndctl, lpsz)Hwndctl is the handle of this combo box. lpsz is the string to be added. The combo box cannot be added directly by ID. Therefore, you must use the getdlgitem () function to obtain the handle of this control. for example, hwnd hwndcombo1 = getdlgitem (hwnd, idc_c1); // create a combo box. The ID can be set to idc_c1, and then get the handle of the combo box through getdlgitem. // If the display fails, you need to adjust the maximum display range of this combo box. You can adjust the display range when the up and down arrows appear on the down arrow. combobox_addstring (hwndcombo1, text ("content 1"); combobox_addstring (hwndcombo1, text ("content 2"); // use the idc_c1 handle hwndcombo1 and add the content, you can set multiple texts for multiple calls. // The added content is usually stored in the main_oninitdialog () initialization function. You can also call it by using buttons or other controls. [1] Getting the number of projects
Combobox_getcount (hwndctl)The return value of this function is int. The hwndctl parameter is the control handle of this combo box. For example: hwnd hwndcombo1 = getdlgitem (hwnd, idc_c1); int COUNT = combobox_getcount (hwndcombo1 ); // get the return value by defining count. [1] Delete content
Combobox_deletestring (hwndctl, index)Hwndctl is the handle, index is the project index number, from top to bottom, starting from 0, so 0 is to delete the first item. example: hwnd hwndcombo1 = getdlgitem (hwnd, idc_c1); combobox_deletestring (hwndcombo1, 0); [1] Get Index Number
Combobox_getcursel (hwndctl)The return value of this function is int, and the index number of the Project is returned. example: hwnd hwndcombo1 = getdlgitem (hwnd, idc_c1); int COUNT = combobox_getcursel (hwndcombo1); // obtain the index number. tchar str1 [256]; sprintf (str1, "currently selected index number: % d", count); // use sprintf to store the description text and index number, MessageBox (hwnd, str1, text ("title"), mb_ OK); combobox_deletestring (hwndcombo1, count); // Delete the item that gets this index number. messageBox (hwnd, text ("delete currently selected items"), text ("title"), mb_ OK); // select and delete the current item. If no item is selected, the returned value is-1. specify selected and display values
Combobox_setcursel (hwndctl, index)Example: hwnd hwndcombo1 = getdlgitem (hwnd, idc_c1); combobox_setcursel (hwndcombo1, 2); // select and display the value given by 3rd
Combobox_getlbtext (hwndctl, index, lpszbuffer)Hwndctl handle, index number, and lpszbuffer storage string example: hwnd hwndcombo1 = getdlgitem (hwnd, idc_c1); cstring STR; combobox_getlbtext (hwndcombo1, 2, STR); // values are not returned, the string is actually stored and passed through Str. messageBox (hwnd, STR, text ("title"), mb_ OK ); ========================================================== ========================================================== ======
1. How to add/delete combo box content
1. Add a row in the data label of the combo box control property, indicating a row in the combo box drop-down list. Press Ctrl + enter to wrap the text.
2. Dynamic addition during program Initialization
For example: // control content Initialization
Cstring strtemp;
(Ccombobox *) getdlgitem (idc_combo_cf)-> resetcontent (); // eliminate all existing content
For (INT I = 1; I <= 100; I ++)
{
Strtemp. Format ("% d", I );
(Ccombobox *) getdlgitem (idc_combo_cf)-> addstring (strtemp );
}
3. Add
For example, cstring strtemp;
Int icount = (ccombobox *) getdlgitem (idc_combo_cf)-> getcount (); // obtain the number of existing rows
If (icount <1) // prevents repeated addition
{
(Ccombobox *) getdlgitem (idc_combo_cf)-> resetcontent ();
For (INT I = 1; I <= 100; I ++)
{
Strtemp. Format ("% d", I );
(Ccombobox *) getdlgitem (idc_combo_cf)-> addstring (strtemp );
}
}
4. Delete
Deletestring (uint nindex) // delete a specified row,
5. Insert
Insertstring (INT nindex, lpctstr lpszitem) // insert the row to the specified position
6. Search
Findstring (INT nstartafter, lpctstr lpszitem) // you can find the specified character transfer position in all current rows. nstartafter indicates to start searching from that row.
Int selectstring (INT nstartafter, lpctstr lpszitem) // You can select a row that contains the specified string.
Ii. How to control the combo box drop-down Length
1. First, you need to know two points: 1. On the design page, click the drop-down arrow of the combo box. The box that appears is the drop-down box of the combo box.
2. There is a no integral height hook option in the attribute, indicating that the maximum length is the design length. If the actual content is longer than the design length, a scroll bar appears and the actual length is displayed if the content is smaller.
3. Select a line
1. Select:
Int IPOs = (ccombobox *) getdlgitem (idc_combo_cf)-> getcursel (); // the currently selected row.
2. Set
(Ccombobox *) getdlgitem (idc_combo_cf)-> setcursel (n) // you can specify the content of row N as the displayed content.
4. Get the combo box content
1. Retrieve the current content
(Ccombobox *) getdlgitem (idc_combo_cf)-> getwindowtext (strtemp );
2. Retrieve other row content
(Ccombobox *) getdlgitem (idc_combo_cf)-> getlbtext (n, strtemp );
Iv. Focus
You can use the getfocus () function to determine whether the control obtains the focus.
For example, if (getfocus () = getdlgitem (idc_edit_value2) // checks whether the focus is in the idc_edit_value2 edit box.
But the focus of ComboBox is different, because it is composed of edit and ListBox.
Therefore, getparent (): If (getfocus ()-> getparent () = getdlgitem (idc_combo_cf) is used to obtain the focus ))
Control the combo box drop-down Length
1. First, you need to know two points: 1. On the design page, click the drop-down arrow of the combo box. The box that appears is the drop-down box of the combo box.
2. There is a no integral height hook option in the attribute, indicating that the maximum length is the design length. If the actual content is longer than the design length, a scroll bar appears and the actual length is smaller.
VC ++ combo box/Combo box ex Control
A combination window consists of an input box and a list box. You can use the member functions to create a combination window:
Bool clistbox: Create (lpctstr lpsztext, DWORD dwstyle, const rect & rect, cwnd * pparentwnd, uint nid = 0 xFFFF );
Dwstyle indicates the style of the window. In addition to the commonly used style ws_child and ws_visible of the sub-window, you can specify a special style for the list control.
Cbs_dropdown drop-down box
Cbs_dropdownlist drop-down box, but cannot be input in the input box
The cbs_simple input box and the list box are both displayed.
All lbs_sort rows are sorted alphabetically.
Because a list box contains a list box, the function of the list box can be used, such:
Int addstring (lpctstr lpszitem) to Add rows,
Int deletestring (uint nindex) deletes a specified row,
Int insertstring (INT nindex, lpctstr lpszitem) inserts the row into the specified position.
Void resetcontent () can delete all rows in the list box.
Call int getcount () to obtain the number of rows in the current list box.
To obtain/set the position of the currently selected row, call int getcursel ()/INT setcursel (INT iindex ). Call int getlbtext (INT nindex, lptstr lpsztext) to obtain the string of the specified row in the list box.
In addition, you can call int findstring (INT nstartafter, lpctstr lpszitem) to find the specified character transfer position in all current rows. nstartafter indicates to start searching from that row.
Int selectstring (INT nstartafter, lpctstr lpszitem) can be used to select rows containing the specified string.
In addition, the functions of the input box can be used, such:
DWORD geteditsel ()/bool seteditsel (INT nstartchar, int nendchar) to obtain or set the position of the selected character in the input box.
Bool limittext (INT nmaxchars) sets the maximum number of characters that can be entered in the input box.
The clipboard function of the input box is copy, clear, cut, and paste.
At last, we will introduce several common message ing Macros in the list box:
On_cbn_dblclk double-click
The on_cbn_dropdown list box is displayed.
On_cbn_killfocus/on_cbn_setfocus is generated when the input box loses/gets the input focus
The row selected in the on_cbn_selchange list box is changed.
The content in the on_cbn_editupdate input box is updated.
The preceding message ing methods are used to define the function of the prototype afx_msg void memberfxn (); and the message ing in the form of on_notification (ID, memberfxn. If a combo box is used in the dialog box, class wizard automatically lists related messages and generates message ing code.
The combo box is enhanced in MFC 4.2. You can use imagelist In the combo box. A new class ccomboboxex (derived from ccombobox) is provided to implement this function. Added some new member functions in the ccomboboxex class to implement new functions: first, you need to call cimagelist * setimagelist (cimagelist * pimagelist) to set imagelist, and then call
Int insertitem (const comboboxexitem * pcbitem); To Add rows. The comboboxexitem is defined as follows:
Typedef struct {
Uint mask;
Int iItem;
Lptstr psztext;
Int cchtextmax;
Int iimage;
Int iselectedimage;
Int ioverlay;
Int iindent;
Lparam;
} Comboboxexitem, * pcomboboxexitem;
You need to set mask = cbeif_image cbeif_text, set iItem to insert location, set psztext to display string, and set iimage to display icon index. The following code demonstrates how to insert:
/* M_cbewnd is the created ccombobox object
M_list: the bitmap of the cimagelist object idb_img is 16 * (16*4), and each image is 16*16, with 4 icons in total */
M_list.create (idb_img, 16, 4, RGB (0, 0 ));
M_cbewnd.setimagelist (& m_list );
Comboboxexitem insitem; insitem. mask = cbeif_image cbeif_text; insitem. iItem = 0; insitem. iimage = 0; insitem. psztext = "Line 1"; m_cbewnd.insertitem (& insitem); insitem. iItem = 1; insitem. iimage = 1; insitem. psztext = "Line 2"; m_cbewnd.insertitem (& insitem );
Call int deleteitem (INT iindex); to delete a row and specify its position.
You can call bool getitem (comboboxexitem * pcbitem)/bool setitem (const comboboxexitem * pcbitem) to obtain/set row data.
5. How can I display the default value of the ccombobox control?
Add m_constants_combo.setcursel (4) during initialization, or connect to a variable in the Class Wizard.