How to Use a Listbox Control in MFC Visual C++

A list box is a Windows control that can display text or iconic items as a list of rows. The Microsoft Foundation Class (MFC) Library offers the CListBox class that encapsulates all the necessary functionalities for manipulating a list box. Follow these steps to see how you can add a text string to and remove it from a list box in Microsoft Visual Studio.

Things You'll Need

  • Microsoft Visual Studio IDE
  • Book on MFC, such as "Programming Windows With MFC" by Jeff Prosise
Show More

Instructions

    • 1

      Create a new project in Visual Studio to manipulate the control. From the upper menu bar, click "File" > "New" and select "MFC AppWizard(exe)" in the "Projects" tab. Enter a name for the project in the "Project name" text box and click "OK." Select the "Dialog based" radio button and click "Finish" and then "OK." A dialog screen with two control buttons, one "OK" and one "Cancel," is displayed in the Design View mode.

    • 2

      Add MFC controls on the dialog screen: one list box and some auxiliary controls to make the tutorial more interesting. To find a list box, move the mouse over the controls toolbox and read the popup balloons. Click the "List Box" icon and click on the dialog screen to add it there. In a similar fashion add an "Edit Box" and two "Buttons."

    • 3

      Modify the MFC controls. Right-click on the list box, select "Properties" and change the ID to IDC_MYLISTBOX, under the "General" tab. Do the same to the edit box and change its ID to IDC_MYEDITBOX. In one button, change the ID to IDC_MYBUTTONADD and the Caption to "Add." In the other button, change the ID and Caption to IDC_MYBUTTONREM and "Remove," respectively.

    • 4

      Join the list box and edit box using the Class Wizard. From the upper menu, click "View" > "Classwizard" or press Ctrl+W. Select "IDC_MYLISTBOX" under the "Member Variables" tab and click "Add Variable." Type "m_myListBox" in the "Member variable name" text box and select "Control" under "Category." Click "OK." Next, select "IDC_MYEDITBOX," click "Add Variable" and give the name "m_myEditBox." Don't change the combo boxes. Click "OK" to close the class wizard.

    • 5

      Give some functionality to the Add button. Double-click the "Add" button. When the "Add Member Function" window appears, click "OK." Copy and paste the following code inside the "::OnMybuttonadd()" function, under the comment:


      CString str;

      UpdateData();

      str = m_myEditBox;

      UpdateData(FALSE);

      m_myListBox.AddString(str);
    • 6

      Repeat Step 5 for the "Remove" button. Copy and paste the following code inside the "::OnMybuttonrem()" function, under the comment:


      int pos;

      CString str;

      pos = m_myListBox.GetCurSel();

      m_myListBox.DeleteString(pos);
    • 7

      Compile and run the code. Type some text in the edit box and click the Add button. That text will be added as a row in the list box. Next, highlight a list box row and click Remove. This action deletes the row.

Tips & Warnings

  • The list box communicates with its parent, usually a dialog box like in this tutorial, by means of notification messages like LBN_DBLCLICK (the list box has been double-clicked).

  • The CListBox class offers many other member functions that fall into seven categories. Here are just a few:

    Construction
    CListBox
    Initialization
    Create
    InitStorage
    General Operations
    GetCount
    GetItemRect
    GetItem
    Single-Selection Operations
    GetCurSel
    Multiple-Selection Operations
    SetSel
    SetItemRange
    String Operations
    AddString
    FindString
    ResetContent
    Overridables
    DrawItem
    For a complete listing, visit Microsoft.com and enter "CListBox" in the search box.

Related Searches:

Resources

Comments

  • Johannekie Jul 15, 2008
    Hi I want to know how to use the multi column listbox. How do you insert text (comma seperated) into the listbox so that the text is then seperated by columns.
  • Johannekie Jul 15, 2008
    Hi I want to know how to use the multi column listbox. How do you insert text (comma seperated) into the listbox so that the text is then seperated by columns.

You May Also Like

  • How to Create Columns in Listbox C#

    C# programming language allows the inclusion of Microsoft .NET components, such as the ListBox form control in the user applications. The ListBox...

  • ListBox Visual Basic Tutorial

    The ListBox control in Visual Basic is a powerful tool for displaying lists of data and allowing the user to select one...

  • How to Manipulate Strings in MFC

    The STL Library of pure C++ has the "string" class. The Microsoft Foundation Class (MFC) Library has its own salvation: the CString...

  • How to Use a Listbox in Visual Basic .Net

    The ListBox control in Visual Basic has some powerful features, making it useful in a number of applications. For example, the programmer...

  • How to Manipulate List View Controls in MFC

    List view controls are the controls that are used to show data in different formats. These formats are icon view, report view...

  • How to Use Columns in a Listbox

    Listboxes are form elements that display an array of options for the user. The most prominent use for a listbox is its...

  • How to Use Edit Controls in MFC

    In Microsoft Windows based systems the CEdit class provides the functions of the edit controls in MFC. In a dialog based program...

  • How to Add Glut With Visual C

    The OpenGL Utility Toolkit (GLUT) is a window system independent toolkit for writing graphics applications. It allows the programmer to quickly create...

  • How to Add a ListBox

    The ListBox control is a programming graphical tool that shows a list of text items in a Windows application. The user can...

Related Ads

Featured