How to Use a Combo box in MFC
A combo box control shows a list of strings in drop-down menu style. Providing a way to show multiple options while saving space, a combo box combines the functionality of a list box and an edit box. The capabilities of the combo box control are encapsulated in the CComboBox class of the Microsoft Foundation Class Library. This tutorial demonstrates some commonly used features of this class.
Things You'll Need
- Microsoft Visual Studio IDE
- Book on MFC, such as "Programming Windows with MFC" by Jeff Prosise
Instructions
-
-
1
Launch Microsoft Visual Studio. Generate a dialog-based application for this project by clicking File > New > Projects (tab), and selecting "MFC AppWizard (exe)." Type "MyCombobox" under "Project name." Press OK, select "Dialog based" and click Finish.
-
2
Create a combo box and modify its properties. Click the Resources tab on the left side of the workspace. Expand the Dialog folder and select IDD_MYCOMBOBOX_DIALOG. Click the combo box icon from the toolbox on the right side and put it on the dialog. Resize the drop-down extend of the combo box by clicking on the arrow and dragging down the rectangle. Then right-click, select Properties, click the Styles tab and, under Type, select Simple. Close the Combo Box Properties box.
-
-
3
Add items to the combo box. You can do this in two ways. To add them at design time, right-click on the combo box, select Properties, click the Data tab and enter the items. To get to the next item, press Ctrl+Enter. To add them at runtime, call the CComboBox::AddString() for adding items to the end or CComboBox::InsertString() for inserting items in specific location. The items are of type lpszString.
-
4
Change the width of the combo box dynamically. You can display any item fully, no matter how long it is, thanks to the CComboBox::SetDroppedWidth() member function. It takes as parameter the width of the string in pixels.
-
5
Perform other common functions. Select an item at runtime by using CComboBox::SetCurSel(). It accepts as parameter an integer index. Find the index of the selected item with CComboBox::GetCurSel(). Retrieve data by using CComboBox::GetItemData(). Determine the item count through CComboBox::GetCount().
-
6
Eliminate items from an MFC combo box. If the string is known, you can use CComboBox::FindString() to determine the index and then use CComboBox::DeleteString(). CComboBox::Clear() deletes the current selection if any, while CComboBox::Cut() removes the current selection and places it on the clipboard.
-
7
Understand the message-map notification messages that a combo box can send. When the combo box's list box is closed, for example, ON_CBN_CLOSEUP is sent. When the combo box loses its input focus because of a mouse click outside its boundaries, it sends ON_CBN_KILLFOCUS. There's a message-map for every possible state-change that a combo box can undergo.
-
1
Tips & Warnings
The only time you have to worry about destroying a combo box is if you create it on the heap. If you call the "new" operator, you need to call "delete" at cleanup.
Resources
Comments
-
Jimmy Salgado
Feb 07, 2011
how do i compare the content selected in the combobox???.. or how do i make sure that the user selects one of the options in the combobox?? -
auliac
Dec 15, 2009
if the items of combo box are the record of database. how can i built it?