Original URL: http://blog.csdn.net/kinglimy/article/details/6452239
Combo Box (combo box) controls are simple and can save space. From the user's point of view, this control is made up of a text input control and a drop-down menu. Users can select an option from a pre-defined list, and can also enter text directly into the text box. The following example briefly shows how to manipulate a list of strings using MFC CComboBox class.
1. Define the control's corresponding variable
Suppose you have created a dialog and dragged the Combo Box control from the Control Toolbox onto it. Open the Class Wizard and add the corresponding variables for the control, such as: CComboBox m_cbexamble;
This variable is used continuously in the following code.
2. Add Items to the control
1) Add the combo box control property to the data tag, and a row represents a row in the combo box drop-down list. Line break with Ctrl + ENTER.
2) Use the function addstring () to add Items to the Combo Box control, such as:
M_cbexample.addstring ("StringData1");
M_cbexample.addstring ("StringData2");
M_cbexample.addstring ("StringData3");
3) You can also call the function insertstring () to insert the Item into the specified position nIndex, such as:
M_cbexample.insertstring (NIndex, "stringdata");
3. Get the selected item from the control
Assuming that an item is already selected in the list of controls, now to get the contents of the selected item, first to get the position of the item, and then to get the contents of the corresponding position. Two functions are used here, such as:
int nIndex = M_cbexample.getcursel ();
CString Strcbtext;
M_cbexample.getlbtext (NIndex, Strcbtext);
In this way, the resulting content is stored in the Strcbtext.
To pick the current content, you can call function GetWindowText (strcbtext).
4. Find the given item in the control
This operation is typically used to dynamically modify the value of the item in the program, and can be precisely matched with the function findstringexact (), such as:
int nIndex = M_cbexample.findstringexact (Nstartafter, "value to be found");
Nstartafter indicates from which row to start the lookup. If the lookup succeeds, the location of the item is returned; otherwise, the CB_ERR is returned.
You can also select the item that contains the specified string, such as:
int nIndex = m_cbexample.selectstring (Nstartafter, "value to be selected");
5. Delete the item in the control
This operation can take advantage of the function deletestring (), which specifies the location of the deleted item, such as:
M_cbexample.deletestring (NIndex);
You can also use the function resetcontent () to clear all current items, such as:
M_cbexample.resetcontent ();
6. Display an item in the control
int nIndex = M_cbexample.getcursel (); The currently selected item
M_cbexample.setcursel (NIndex); Set the contents of item nindex to be displayed
7. Get or set the selected character position in the input box
DWORD Geteditsel ()/bool seteditsel (int nstartchar, int nendchar);
BOOL limittext (int nmaxchars); Sets the maximum number of characters that can be entered in the input box.
8. List box common message map macros
ON_CBN_DBLCLK Mouse Double-click
On_cbn_dropdown list box is popped
On_cbn_killfocus/on_cbn_setfocus generated when the input box loses/gets input focus
On_cbn_selchange the selected row in the list box has changed
On_cbn_editupdate the contents of the input box are updated
Add:
First, how to add/Remove combo box content
1, added in the Data tab of the combo box control property, one row represents a row in the combo box drop-down list. Line break with Ctrl + ENTER.
2, dynamically added during program initialization
such as://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, drop-down when adding
such as: CString strtemp;
int icount= ((ccombobox*) GetDlgItem (IDC_COMBO_CF))->getcount ();//Get the current number of rows
if (icount<1)//Prevent repeating multiple additions
{
((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 the specified line,
5, inserting
insertstring (int nIndex, LPCTSTR lpszitem)//Inserts a row into the specified position
6, find
FindString (int nstartafter, LPCTSTR lpszitem)//The position of the specified word nginx can be found in all current rows, nstartafter indicates that the lookup is started from that line.
int selectstring (int nstartafter, LPCTSTR lpszitem)//You can select the row containing the specified string
Second, how to control the combo box's drop-down length
1, first of all to know two points: one, that is, in the design interface, click on the combo box's drop-down arrow, the adjustment box that appears is combo box's drop-down adjustment frame.
2, two, the attribute has a No integral height hook option, indicating that the maximum length is the design length, if the actual content than the design length, there will be a scroll bar, less on the actual length of display.
Third, select one of the lines
1, select:
int ipos= ((ccombobox*) GetDlgItem (IDC_COMBO_CF))->getcursel ();//The currently selected row.
2, set
((ccombobox*) GetDlgItem (IDC_COMBO_CF))->setcursel (n)//Set the contents of the nth row to be displayed.
Iv. Get combo Box contents
1 taking the current content
((ccombobox*) GetDlgItem (IDC_COMBO_CF))->getwindowtext (strtemp);
2 Fetching Other line contents
((ccombobox*) GetDlgItem (IDC_COMBO_CF))->getlbtext (n,strtemp);
Iv. gaining focus
The GetFocus () function can often be used to determine whether a control has the focus
For example: if (GetFocus () ==getdlgitem (idc_edit_value2))//Determines whether the focus is within the edit box idc_edit_value2.
But the focus of the ComboBox is different because it is made up of the edit and listbox two parts
So get the focus to use GetParent (): If ((GetFocus ()->getparent ()) ==getdlgitem (IDC_COMBO_CF))
Control the drop-down length of the combo box
1, first of all to know two points: one, that is, in the design interface, click on the combo box's drop-down arrow, the adjustment box that appears is combo box's drop-down adjustment frame.
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 more than the design length, the scroll bar appears, and less is displayed in the actual length.
VC + + Combo Box/combo Box ex Control
A combined window consists of an input box and a list box. To create a composite window, you can use member functions:
BOOL clistbox::create (LPCTSTR lpszText, DWORD dwstyle, const rect& RECT, cwnd* pparentwnd, UINT NID = 0xFFFF);
Where dwstyle will indicate the style of the window, in addition to the style ws_child,ws_visible commonly used in the Subwindow, you can specify a specific style for the list control.
Cbs_dropdown drop-down combo box
Cbs_dropdownlist drop-down combo box, but cannot be entered in the input box
Cbs_simple input box and list box are displayed at the same time
Lbs_sort all rows in alphabetical order
Because the combo box contains a list box, the functionality of the list box can be used, as available:
int addstring (LPCTSTR lpszitem) adds a row,
int deletestring (UINT nIndex) deletes the specified line,
int insertstring (int nIndex, LPCTSTR lpszitem) inserts a row into the specified position.
void Resetcontent () can delete all rows in the list box.
The number of rows in the current list box is obtained by calling int GetCount ().
If you need to get/set the position of the currently selected row, you can call int GetCurSel ()/int setcursel (int iIndex). The string of the specified row within the list box is obtained by calling int getlbtext (int nIndex, LPTSTR lpszText).
In addition, by calling int FindString (int nstartafter, LPCTSTR lpszitem) to find the position of the specified word nginx in all current rows, nstartafter indicates that the lookup is started from that line.
int selectstring (int nstartafter, LPCTSTR lpszitem) can select the line containing the specified string.
In addition, the function of the input box can be used, if available:
DWORD Geteditsel ()/bool seteditsel (int nstartchar, int nendchar) Gets or sets the character position that is selected 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 Copy,clear,cut,paste can be used.
Finally, we introduce some common message map macros in the list box:
ON_CBN_DBLCLK Mouse Double-click
On_cbn_dropdown list box is popped
On_cbn_killfocus/on_cbn_setfocus generated when the input box loses/gets input focus
On_cbn_selchange the selected row in the list box has changed
On_cbn_editupdate the contents of the input box are updated
Use the following methods of message mapping to define a prototype such as: afx_msg void Memberfxn (), a function that defines the form such as on_notification (ID, MEMBERFXN) of the message map. If you use a combo box in a dialog box, the Class Wizard automatically lists the related messages and automatically generates the message map code.
The combo box has been enhanced in MFC 4.2, and you can use ImageList in the combo box to have a new class CComboBoxEx (derived from CComboBox) to implement this functionality. Some new member functions have been added to the CComboBoxEx class to implement the new functionality: first you need to call cimagelist* SetImageList (cimagelist* pimagelist), set ImageList, and then call
int InsertItem (const comboboxexitem* Pcbitem); To add a row, where Comboboxexitem is defined as follows:
typedef struct {UINT mask; int iItem; LPTSTR PszText; int Cchtextmax; int iImage; int iselectedimage; int ioverlay; int iindent; LPARAM LPARAM;} Comboboxexitem, *pcomboboxexitem;
You need to set the Mask=cbeif_image Cbeif_text and set the iitem as the insertion position, set the Psztext as the display string, and set the Iimage as the icon index for the display. The following code shows how to insert:
/*m_cbewnd for CComboBox objects that have already been created
M_list for CImageList object idb_img to 16* (16*4), each picture is 16*16 a total of 4 icons */
M_list. Create (Idb_img,16,4,rgb (0,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);
The row is deleted by calling int DeleteItem (int iIndex), and the position of the row is indicated.
Get/Set row data by calling BOOL GetItem (comboboxexitem* pcbitem)/bool setitem (const comboboxexitem* pcbitem);
The use of the "Go" MFC drop-down list box