Virtual Developer Workshop: Containerized Development with Docker

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


  • Listbox

    Posted by Legacy on 01/28/2004 08:00am

    Originally posted by: poor

    Thanks a lot!

  • Multicolumn ListBox

    Posted by Legacy on 08/29/2002 07:00am

    Originally posted by: harish

    thanx a lot for this Customized CDBlistBox class

  • Thanks a lot for your Example

    Posted by Legacy on 04/22/2001 07:00am

    Originally posted by: BLaZe

    I just want to say you my gratitude for your source, now I Can Load&Save Data to a database and load it in a CListCtrl in REPORT MODE ! :-)



  • Custom ListBox with columns work around for others

    Posted by Legacy on 12/17/1999 08:00am

    Originally posted by: Charles Curtis

    I have completed the first portion of my project which I modified and used some of the code from your project. So far it is working GREAT! Your project has saved me many hours of frustration and it looks like I will be able to do everything I want. I had a very good start which means I will have a lot more time to spend adding the functionality I want without having to fight all the stuff you already have covered so well!

    I also wanted to bring something to your attention that may help you or some other poor soul trying to do the same thing we are. Item 4. on your page you said:

    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.

    Well I didn't use any of the CDaoRecordset as my project uses Oracle for the Database. So I would have had a difficult time trying to figure out how to get the CDaoRecordset stuff working. So I Used the CRecordset and in the OnFind portion of the code I changed the Clients.m_strFilter =
    "SurName LIKE '" + m_SurName + "*' " +
    "AND SecondName LIKE '" + m_SecondName + "*' " +
    "AND Address LIKE '" + m_Address + "*' " +
    "AND AddrNo LIKE '" + m_AddrNo + "*' " +
    "AND Phone LIKE '" + m_Phone + "*'";

    to: Clients.m_strFilter = "WHERE ID IS NOT NULL";
    which will (since ID should never be null) produce all records in the table as your LIKE statement does!

    There is NOTHING wrong with the way you handled the problem I just thought you might like to know for future reference and it may help others to know that CRecordSet can be used in the same way as your LIKE!

    Thanks Again for the Excellant Sample!

  • Custom ListBox with columns

    Posted by Legacy on 12/16/1999 08:00am

    Originally posted by: Charles Curtis

    I am VERY happy to have found your Sample project and code here at CodeGuru! Although I will be writing my project from scratch it is a relief (for me) to know that your code will give me a great deal of assistance! You are doing (with your pizza project) exactly what I want to do with my project. Your code will save me MANY hours of frustration as I only have a short time to complete this project! THANK YOU for supplying the code and sample project!


  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date