MFC - List Box

A list box displays a list of items, such as filenames, that the user can view and select. A List box is represented by CListBox class. In a single-selection list box, the user can select only one item. In a multiple-selection list box, a range of items can be selected. When the user selects an item, it is highlighted and the list box sends a notification message to the parent window.
AddString
Adds a string to a list box.
CharToItem
Override to provide custom WM_CHAR handling for owner-draw list boxes which don't have strings.
CompareItem
Called by the framework to determine the position of a new item in a sorted owner-draw list box.
Create
Creates the Windows list box and attaches it to the CListBox object.
DeleteItem
Called by the framework when the user deletes an item from an owner-draw list box.
DeleteString
Deletes a string from a list box.
Dir
Adds filenames, drives, or both from the current directory to a list box.
DrawItem
Called by the framework when a visualaspect of an owner-draw list box changes.
FindString
Searches for a string in a list box.
FindStringExact
Finds the first list-box string that matches a specified string.
GetAnchorIndex
Retrieves the zero-based index of the current anchor item in a list box.
GetCaretIndex
Determines the index of the item that has the focus rectangle in a multipleselection list box.
GetCount
Returns the number of strings in a list box.
GetCurSel
Returns the zero-based index of the currently selected string in a list box.
GetHorizontalExtent
Returns the width in pixels that a list box can be scrolled horizontally.
GetItemData
Returns the 32-bit value associated with the list-box item.
GetItemDataPtr
Returns a pointer to a list-box item.
GetItemHeight
Determines the height of items in a list box.
GetItemRect
Returns the bounding rectangle of the list-box item as it is currently displayed.
GetListBoxInfo
Retrieves the number of items per column.
GetLocale
Retrieves the locale identifier for a list box.
GetSel
Returns the selection state of a list-box item.
GetSelCount
Returns the number of strings currently selected in a multiple-selection list box.
GetSelItems
Returns the indices of the strings currently selected in a list box.
GetText
Copies a list-box item into a buffer.
GetTextLen
Returns the length in bytes of a list-box item.
GetTopIndex
Returns the index of the first visible string in a list box.
InitStorage
Preallocates blocks of memory for list box items and strings.
InsertString
Inserts a string at a specific location in a list box.
ItemFromPoint
Returns the index of the list-box item nearest a point.
MeasureItem
Called by the framework when an ownerdraw list box is created to determine listbox dimensions.
ResetContent
Clears all the entries from a list box.
SelectString
Searches for and selects a string in a single-selection list box.
SelItemRange
Selects or deselects a range of strings in a multiple-selection list box.
SetAnchorIndex
Sets the anchor in a multiple-selection list box to begin an extended selection.
SetCaretIndex
Sets the focus rectangle to the item at the specified index in a multipleselection list box.
SetColumnWidth
Sets the column width of a multicolumn list box.
SetCurSel
Selects a list-box string.
SetHorizontalExtent
Sets the width in pixels that a list box can be scrolled horizontally.
SetItemData
Sets the 32-bit value associated with the list-box item.
SetItemDataPtr
Sets a pointer to the list-box item.
SetItemHeight
Sets the height of items in a list box.
SetLocale
Sets the locale identifier for a list box.
SetSel
Selects or deselects a list-box item in a multiple-selection list box.
SetTabStops
Sets the tab-stop positions in a list box.
SetTopIndex
Sets the zero-based index of the first visible string in a list box.
VKeyToItem
Override to provide custom WM_KEYDOWN handling for list boxes with the LBS_WANTKEYBOARDINPUT style set.
Here are some mapping entries for Listbox −
Let us look into a simple example of List box by creating a new MFC dialog based application.
Step 1 − Once the project is created, you will see the TODO line which is the Caption of Text Control. Remove the Caption and set its ID to IDC_STATIC_TXT.
Step 2 − Drag List Box from the Toolbox.
Step 3 − Add the control variable for the Text control.
Step 4 − Add the Value variable for the Text control.
Step 5 − Add the control variable for the List Box control.
Step 6 − Add the event handler for the List Box control.
Step 7 − Select the LBN_SELCHANGE from the message type and enter name for the event handler.
Step 8 − Add one function, which will load the list box.
void CMFCListBoxDlg::LoadListBox() {
   CString str = _T("");
   for (int i = 0; i<10; i++) {

      str.Format(_T("Item %d"), i);
      m_listBox.AddString(str);
   }
}
Step 9 − Call the function from CMFCListBoxDlg::OnInitDialog() as shown in the following code.
BOOL CMFCListBoxDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();

   // Set the icon for this dialog. The framework does this automatically
   // when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);       // Set big icon
   SetIcon(m_hIcon, FALSE);      // Set small icon

   // TODO: Add extra initialization here
   LoadListBox();
   return TRUE; // return TRUE unless you set the focus to a control
}
Step 10 − Here is the event handler implementation. This will display the current selected item from the List Box.
void CMFCListBoxDlg::OnLbnSelchangeList1() {
   // TODO: Add your control notification handler code here
   m_listBox.GetText(m_listBox.GetCurSel(),m_strItemSelected);
   UpdateData(FALSE);
}
Step 11 − When the above code is compiled and executed, you will see the following output.
Step 12 − When you select any item, it will be displayed on the Text Control.
 Print
 Add Notes
 Bookmark this page