CListBox Example – MFC Tutorial 9

List boxes are used to load a list of items. Users will be able to add, select and delete items with the list boxes.

CListBox Examples:

The MFC class CListBox is the one which encapsulates all the functionalities and members of the List Box control. It can handle almost all the common requirements that a GUI programmer will need with a list box. If at all there are any extra features needed, the programmer can derive a new class from this CListBox and implement his own custom functionality.
There are quite a few basic techniques to be learnt with all the MFC Controls. Some of them related to the List Box control are

  • Retrieving the selected items
  • Adding items
  • Deleting items
  • Handling List box messages

The first job for this MFC tutorial will be to create the MFC Dialog based Application. This application will be used to hold the list boxes. This is the same set of code/steps which are used for the MFC Tutorials explaining about the controls, are being used in this CListBox Example.

To Create the Application:

  1. Open the Microsoft Visual Studio 6.00. Click Menu–> File–> New and Select
    MFC AppWizard(exe) option.
  2. In the next MFC AppWizard screen – Step 1, Choose Dialog based application.
  3. Click Finish after selecting this. All the files needed for this
    CListBox Example MFC Tutorial will be created.
  4. If the project is built and run, it will open a dialog box, with two command buttons and one static display control.
  5. Close the application and come back to the Microsoft Visual studio.

Adding a List box in a Dialog Editor:
The above application and dialog box will be used for handling List box in the tutorial.

The following steps explain how to add a List box for this MFC Tutorial:

  1. Open the dialog editor of the previously created application of this
    CListBox example MFC Tutorial.
  2. Click on the List box from the controls toolbox and place one List box
    control on the dialog.
  3. Open the properties dialog of the List box. This is done by first selecting the
    List box and pressing the “Enter” key.
  4. Rename the ID of the list box as IDC_MYLISTBOX.
  5. Add one edit control with ID as IDC_LISTBOXTEXT.
  6. Add three command buttons, one with ID as IDC_ADD and caption as “Add” and second with ID as IDC_REMOVE and caption as “Remove“. Name the ID of the third one as IDC_GET and the caption as “Get

Now we’ll look at the different ways of manipulating the List boxes. Before doing this, we need to create member variables for the controls. Do the following.

  • Click on the Dialog Editor and Press the Ctrl + W keys or you can instead click on Menu –> View –>Class Wizard to open the Class wizard.
  • Click on the member variables tab
  • Select the IDC_MYLISTBOX id and click Add Variable.
  • Give the variable name as m_ctlListBox and select control in the category combo box.
  • Next, select the IDC_LISTBOXTEXT and click Add Variable. Give the variable name as m_txtListbox and keep the CString selected in the category.
  • Create three different handlers for the command buttons Add,Remove and Get.

Adding Items to the CListBox:

Adding items to a ListBox is done using AddString function. This is a member function of CListBox.
Now, what we’ll do is to add a string in the list box from the Edit box. Whenever we type something in the edit box and click on Add, we’ll add it to the list box. The following is the code snippet required for doing the job.


void CMFC_Tutorial9Dlg::OnAdd()
{

// TODO: Add your control notification handler code here
CString strText;

UpdateData();

strText = m_txtListbox;

UpdateData(FALSE);
m_ctlListBox.AddString(strText); //This is where the actual
data is added
}

The code required for adding to a list box is shown in bold letters. Now build the application and run it. Type some text in the edit control, and press Add button. The text will be added to the list box.

Retrieving Items from the CListBox:
The first step is to get the index of the selected item and then retrieving the name. Look at the CListBox example of
retrieving the item names below. GetCurSel() will be used to retrieve the index
number and GetText() is used to retrieve the text.


void CMFC_Tutorial9Dlg::OnGet()
{

// TODO: Add your control notification handler code here
int index;

CString strText;

index = m_ctlListBox.GetCurSel();
m_ctlListBox.GetText(index,strText);

MessageBox(strText);

}

Deleting Items from the CListBox:
This part will deal with the deleting the selected items from list box. The first step is to get the index of the selected item and then deleting it by using the index. Look at the CListBox example of deleting below.


void CMFC_Tutorial9Dlg::OnRemove()
{
// TODO: Add your control
notification handler code here

      int index;
CString strText;
index = m_ctlListBox.GetCurSel();

m_ctlListBox.DeleteString(index);
}

We can delete the item directly using DeleteString function, if we know the index number the item to
be deleted.


Event Handling:
The list box receives notification messages like the following.

  • LBN_SELCHANGE  – Selection changed
  • LBN_DBLCLICK  – Listbox Double clicked

Let’s now deal with the LBN_SELCHANGE message in this CListBox Example.

  • Open the Control Wizard by Pressing Ctrl + W.
  • On the Message Maps tab, click on the List box from the controls toolbox and place one List on the dialog.
  • You’ll see a list of Messages for the list box on the right side. Select
    LBN_SELCHANGE and click on Add function.
  • Copy and paste the highlighted code into the function.
int index;
CString strText;

index = m_ctlListBox.GetCurSel();
m_ctlListBox.GetText(index,strText);

MessageBox(strText);

When we run the application, on every selection change the list box will display the string selected. Refer to MFC CListBox Example for using Icons in List Box Resource if
Icons are to be added to the listbox.
Summary:

This tutorial on CListBox example discussed

  • Adding items to list box
  • Retrieving items from list box
  • Deleting items
  • Handling Selection change (LBN_SELCHANGE) message

Please find the Sample code here