Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
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.
Sr.No.
Name & Description
1
AddString
Adds a string to a list box.
2
CharToItem
Override to provide custom WM_CHAR handling for owner-draw list boxes which don't have strings.
3
CompareItem
Called by the framework to determine the position of a new item in a sorted owner-draw list box.
4
Create
Creates the Windows list box and attaches it to the CListBox object.
5
DeleteItem
Called by the framework when the user deletes an item from an owner-draw list box.
6
DeleteString
Deletes a string from a list box.
7
Dir
Adds filenames, drives, or both from the current directory to a list box.
8
DrawItem
Called by the framework when a visualaspect of an owner-draw list box changes.
9
FindString
Searches for a string in a list box.
10
FindStringExact
Finds the first list-box string that matches a specified string.
11
GetAnchorIndex
Retrieves the zero-based index of the current anchor item in a list box.
12
GetCaretIndex
Determines the index of the item that has the focus rectangle in a multipleselection list box.
13
GetCount
Returns the number of strings in a list box.
14
GetCurSel
Returns the zero-based index of the currently selected string in a list box.
15
GetHorizontalExtent
Returns the width in pixels that a list box can be scrolled horizontally.
16
GetItemData
Returns the 32-bit value associated with the list-box item.
17
GetItemDataPtr
Returns a pointer to a list-box item.
18
GetItemHeight
Determines the height of items in a list box.
19
GetItemRect
Returns the bounding rectangle of the list-box item as it is currently displayed.
20
GetListBoxInfo
Retrieves the number of items per column.
21
GetLocale
Retrieves the locale identifier for a list box.
22
GetSel
Returns the selection state of a list-box item.
23
GetSelCount
Returns the number of strings currently selected in a multiple-selection list box.
24
GetSelItems
Returns the indices of the strings currently selected in a list box.
25
GetText
Copies a list-box item into a buffer.
26
GetTextLen
Returns the length in bytes of a list-box item.
27
GetTopIndex
Returns the index of the first visible string in a list box.
28
InitStorage
Preallocates blocks of memory for list box items and strings.
29
InsertString
Inserts a string at a specific location in a list box.
30
ItemFromPoint
Returns the index of the list-box item nearest a point.
31
MeasureItem
Called by the framework when an ownerdraw list box is created to determine listbox dimensions.
32
ResetContent
Clears all the entries from a list box.
33
SelectString
Searches for and selects a string in a single-selection list box.
34
SelItemRange
Selects or deselects a range of strings in a multiple-selection list box.
35
SetAnchorIndex
Sets the anchor in a multiple-selection list box to begin an extended selection.
36
SetCaretIndex
Sets the focus rectangle to the item at the specified index in a multipleselection list box.
37
SetColumnWidth
Sets the column width of a multicolumn list box.
38
SetCurSel
Selects a list-box string.
39
SetHorizontalExtent
Sets the width in pixels that a list box can be scrolled horizontally.
40
SetItemData
Sets the 32-bit value associated with the list-box item.
41
SetItemDataPtr
Sets a pointer to the list-box item.
42
SetItemHeight
Sets the height of items in a list box.
43
SetLocale
Sets the locale identifier for a list box.
44
SetSel
Selects or deselects a list-box item in a multiple-selection list box.
45
SetTabStops
Sets the tab-stop positions in a list box.
46
SetTopIndex
Sets the zero-based index of the first visible string in a list box.
47
VKeyToItem
Override to provide custom WM_KEYDOWN handling for list boxes with the LBS_WANTKEYBOARDINPUT style set.
Here are some mapping entries for Listbox −
Message
Map entry
Description
LBN_DBLCLK
ON_LBN_DBLCLK( <id>, <memberFxn> )
The framework calls this member function when list item is double clicked.
LBN_KILLFOCUS
ON_LBN_KILLFOCUS( <id>, <memberFxn> )
The framework calls this member function immediately before losing the input focus.
LBN_SELCHANGE
ON_LBN_SELCHANGE( <id>, <memberFxn> )
The framework calls this member function when selection is changed.
LBN_SETFOCUS
ON_LBN_SETFOCUS( <id>, <memberFxn> )
The framework calls this member function after gaining the input focus.
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.
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.