MFC's ComboBox control usage example tutorial

  • 2020-04-02 02:43:44
  • OfStack

This article describes the use of ComboBox control in MFC in detail in the form of examples. Share with you for your reference. Specific methods are as follows:

I. ComboBox introduction:

The ComboBox control consists of a text input control and a drop-down menu. The user can select an option from a predefined list or type text directly into the text box.
Drag a Combo Box control from the toolbar. Right-click to add a variable named cbBox.

Ii. Usage:

1. Add options to the control, specifying the default options


cbBox.AddString(_T("one"));
cbBox.AddString(_T("two"));
cbBox.AddString(_T("three"));

Each option has a corresponding index number, which starts at 0, just like array index. Since the default sorting method is alphabetical order, the index of one,two,three is 0,2,1

We can also add options to the specified index location


cbBox.InsertString(0,_T("aaa")); //The index of one,two,three becomes 1,3,2

If the dialog box opens, we want the three option to be the default option in combo box.


cbBox.SetCurSel(2); //However, we are unlikely to remember the index of each option, so let's look it up first
int index = cbBox.FindStringExact(0,_T("three")); //If you find an option called three, return its index number. If you don't, return CB_ERR, which is -1.

And in turn we already have an option there to know what its index number is


int index = cbBox.GetCurSel(); 

2. Select an option and get its value

Get the index number and then get the value based on the index number.


CString csValue;
int index = cbBox.GetCurSel();
cbBox.GetLBText(index,csValue);//The values of the options are saved in csValues

An easier way to get a value is to select an item and then display it in the text entry control


cbBox.GetWindowTextW(csValue);

3. Find the options in the list


int index = cbBox.FindStringExact(0,_T("three"));  //Exact search.
int index = cbBox.FindString(0,_T("three"));      //Fuzzy lookups. Threeabc returns an index value if an option is available

What if there are so many options in the drop-down list that you don't want to choose one by one?

At this point, you can enter the first few words of the options in the text box, if the match will be automatically displayed in the text box

The ON_CBN_EDITCHANGE event is first added. The ON_CBN_EDITCHANGE message mapping macro binds to a function a message that changes the input value in the text box

Then add the following code to the message handler:


CString temp;
cbBox.GetWindowTextW(temp); //Place the value entered in the text field into a variable called temp
int n =cbBox.FindString(0,temp); //Find out if there is an option in the drop-down list that contains the string temp
if(n>=0)
{
 cbBox.SetCurSel(n);  //If found, the name of the option is displayed in the text box
}

4. Delete options

Delete all options or leave the drop-down list blank


cbBox.ResetContent();

Delete an option. You can only find the corresponding index number, and then delete according to the index number


int index = cbBox.FindStringExact(0,_T("three"));
cbBox.DeleteString(index);

5. Do not type in ComboBox

Sometimes we want to be able to select only in a drop-down box and not allow typing in a ComboBox. Simply change the style to Drop List

6. Response message

Sometimes you want to respond to a message after you select a later value in the ComboBox. It's also easy to just add a response to ComboBox's CBN_SELCHANG event.

7. Can't show drop-down content how to solve

Set the ComboBox height in the OnSize() function.
To determine if the window has been created, use GetDlgItem(IDC_COMBO1)- > GetSafeHwnd() determines that being true means that it has been created.
Then use MoveWindow() to resize the control, as high as possible, because it adjusts automatically depending on the content. This will fully display the contents of the drop-down list.

8. The Extended Combo Box option has an icon in front of it

An Extended Combo Box works a little more than a Combo Box, where you can add an icon to an option.

First, add an Extended Combo Box control and add a variable cbBoxE to it.


CImageList* pImageList;   //It's like a special array, but it holds the picture
pImageList = new CImageList();
pImageList->Create(32, 32, ILC_COLOR32, 0, 4); //
//Add a BMP image to the resource. Id is IDB_BITMAP1
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP1); //Load the image in the resource
pImageList->Add(&bmp, RGB(0, 0, 0)); //Add image to imagelist
cbBoxE.SetImageList(pImageList);
COMBOBOXEXITEM insItem; //It is a structure, and the following is the initialization of some of the variables in it
insItem.mask = CBEIF_IMAGE|CBEIF_TEXT;
insItem.iItem=0;
insItem.iImage=0;
insItem.pszText=_T("Line 1");
cbBoxE.InsertItem(&insItem);//Adds an option to the control, and the option is preceded by a picture

I believe that this paper has a certain reference value for everyone to learn VC++ MFC program design.


Related articles: