11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5. Common Controls<br />

In this function, first the number of selected items is retrieved by calling function<br />

CListBox::GetSelCount(), and the retrieved value is saved to variable nSelSize. If the size is not zero,<br />

we allocate an integer type array with size of nSelSize. Then by calling function<br />

CListBox::GetSelItems(…), we fill this buffer with the indices of selected items. Next, a loop is used to<br />

retrieve the text of each item. The procedure of retrieving selected text for list box IDC_LIST_DIR is the<br />

same.<br />

5.8 Combo Box<br />

Combo box is another type of common control that allows the user to select one object from a list.<br />

While a list box allows multiple selections, a combo box allows only single selection at any time. A combo<br />

box is made up of two other controls: an edit box and a list box. There are three types of combo boxes: 1)<br />

Simple combo box: the list box is placed below the edit box, and is displayed all the time; the edit box<br />

displays the currently selected item in the list box. 2) Drop down combo box: the list box is hidden most of<br />

the time; when the user clicks the drop-down arrow button located at the right corner of the edit box, the list<br />

box is shown and can be used to select an item. In both 1) and 2), the edit box can be used to input a string.<br />

3) Drop list combo box: it is the same with drop down combo box, except that its edit box cannot be used to<br />

input string.<br />

Using a combo box is more or less the same with that of a list box. We must first create combo box<br />

resources in the dialog template, set appropriate styles, then in the dialog’s initialization stage (in function<br />

CDialog::OnInitDialog()), initialize the combo box. We can add message handlers to trap mouse or<br />

keyboard related events for combo box. Two most important messages for combo boxes are CBN_CLOSEUP<br />

and CBN_SELCHANGE. The first message indicates that the user has clicked the drop-down arrow button,<br />

made a selection from the list box, and the drop down list is about to be closed. The second message<br />

indicates that the user has selected a new item.<br />

In <strong>MFC</strong>, combo box is supported by class CComboBox. Like CListBox, class CComboBox has a function<br />

CComboBox::AddString(…) which can be used to initialize the contents of its list box. Besides this, we can<br />

also initialize the contents of a list box when designing dialog template. In the property sheet whose caption<br />

is “Combo Box Properties”, by clicking “Data” tab, we will have a multiple-line edit box that can be used<br />

to input initial data for combo box. We can use CTRL+RETURN keys to begin a new line (Figure 5-8).<br />

Class CComboBox has two functions that allow us to change the contents contained in the list box<br />

dynamically: CComboBox::InsertString(…) and CComboBox:: DeleteString(…).<br />

When designing drop-down combo box, we must set its vertical size, otherwise it will be set to the<br />

default value zero. In this case, there will be no space for the list box to be dropped down when the user<br />

clicks drop down button. To set this size, we can click the drop-down button in the dialog template. After<br />

doing this, a resizable tracker will appear. The initial size of a combo box can be adjusted by dragging the<br />

tracker’s border (Figure 5-9).<br />

Click Data tab<br />

Initialize list box<br />

contents<br />

Figure 5-8. Initialize combo box contents<br />

105

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!