Custom ListBox with columns

Download Source Code and Example

This article describes a custom a list box with columns. I will explain how
the program works, but for a better understanding you should read the source
code. This source is part of a big project that has to do with pizzas and
that’s why it contains the pizza word in several places.

  1. In the OnInitDialog() function I am initializing the ClientsLB,
    which is of type CDBListBox. I am getting the positions of some
    dialog controls and I am using this information to position my
    list box at the correct place. The actual initialization of the
    ClientsLB is done by calling:


    ClientsLB.Init( 20, tmpRect2, this, IDC_CLIENTLB );

    The first parameter is the height of the list box items
    The second its position
    The third the parent window
    The last is the controls id

    Then I am adding tab stops to the control, meaning that I am
    telling the control where to put each column. This is done by calling


    SetTabStop( short ColumnIndex, short ColumnPosition )

    In this application I am using the position of the edit controls
    above my list box in order to set the tabs position.

  2. The control is now ready to accept data. This is done by calling the
    CListBox::AddString() but the string must have a special format. It
    contains all the columns seperated by #. For example:


    AddString( “column1#column2#column3” )

    The control will extract every column and will display it at the
    appropriate position, as given by SetTabStop().

    The control has a function called GetCurrentID(). This function
    returns the first column of the selected item as a CString, it is
    assumed that this is an id column. If there is no current selection
    in the list box, it returns “NS”.

    It also has a function called ExtractString(). You must call this
    function twice. The first time with CMD_RESET as the first parameter
    and a string to analyze as the second. The string is copied to the
    internall buffer and then by calling the function again with CMD_NEXT
    as the first parameter we are getting the extracted CString in the
    second. Every time called it returns the next column of the
    given string. The function returns the index of the extracted column
    or -1 if there is not another column in the given string.

    So by using the AddString we are adding data to the listbox and
    by using GetText we are getting a string from it. By calling ExtractString
    we are getting the columns of the above string.

  3. The list box is connected to the edit controls above it. This is done
    by handling the ON_LBN_SELCHANGE() event. You can read the
    CPizzaDlg::OnSelchange() in order to understand how we can use
    ExtractString().

    You can read the CPizzaDlg::OnAddNew() in order to see how we can
    format the strings we add to the list box.

  4. I am using the AddNew() and Update() to add a record to my database
    I am using SQL commands to delete, modify and find records. I am using
    CDaoRecordset because when I used CRecordset I found that there was
    a problem when I used LIKE ‘*’ in my SQL question. CDatabase was
    searching for the * in my database, didn’t used it as a balanter.

  5. CPizzaDlg::PopulateLB() is an example of how you can populate the
    list box with database records.

  6. The sample application is a simple addresser. To find a record you
    give an id or any other information ( SurName, SecondName, … ), you
    can give only the first letters. If you give the id the other fields
    are ignored. Then press find. When pressing the Add Button, you are
    adding a record, and Delete deletes it. You can also modify the
    selected record. If you want to see all the records just leave the
    edit boxes empty and press the find button.

    Last updated: 4 July 1998

More by Author

Must Read