Multi column Combo box (3)

  Environment: VC++ 4.2 and above, Win95/98

When I saw the article "Multicolumn Combobox" by Jakawan Ratiwanich. Besides issues in comment, I noticed that his program still had three issues. First, items in ComboBox can not be sorted. Second, all items of the selected row are showed in edit control box. Third, if a combo box has more than 5 columns, user must modify the class.

When I saw the article "A Multi Column Combobox(2)" by Mihai Filimon. Besides issues in comment, I noticed that his program still had other three issues. First, the class is very complex for normal user to use or modify. Second, it's not same as normal combobox class, user must understand how it works before using it. Third, user can hide key column, but key item selected is showed in edit control box.

So, I want to show you my class CMultiColumnComboBox used in my project. It is a owner-draw combo box class, derived from class CComboBox, but it's very easy to use. As showed above, it's works exactly like a normal combo box. No matter how many columns a combo box has, you do not need to modify the class.

You can set a column to be shown and get text from BoundColumn, even a combo box is "drop down" one or "simple" one. When you key in chars in the edit box of "drop down" or "simple" combo box, items are searched in ShowColumn, not BoundColumn. You also can control the text color in disabled control.

How user can use this control?

  1. Put an Ownerdraw ComboBox in your resource dialog.
  2. Add new member variables dialog class.
	DDX_Control(pDX, IDC_COMBO1, m_ComboBoxControl1);
	DDX_CBString(pDX, IDC_COMBO1, m_ComboBoxString1);
  1. In message map function OnInitDialog, use FormaComboBox to set m_TotalColumn, m_BoundColumnn and m_ShowColumn, this function must be called brfore other function called.
	m_ComboBoxControl2.FormatComboBox(2, 0, 1);
  1. Then, use SetColumnWidth to set m_ColumnWidth for each column, use SetColumnAlignStyle to set m_ColumnAlignStyle for each column. If default value is used, skip them.
	m_ComboBoxControl2.SetColumnWidth(150, 0);
	m_ComboBoxControl2.SetColumnAlignStyle(DT_LEFT, DT_CENTER);
  1. Use CMultiColumnComboBox::AddRow instead of CComboBox::AddString to add a row to the list box of a combo box.
	m_ComboBoxControl2.AddRow(ColumnString1, ColumnString0);
	m_ComboBoxControl4.AddRow(ColumnString0, ColumnString1, ColumnString2, ColumnString3);
  1. As you see, now you can use it as a normal combo box.

Notes:

Function SetColumnWidth, SetColumnAlignStyle and AddRow can be called with a variable number of arguments. You can modify AddRow to works as Printf.

If you need more help, free feel to ask me.

Downloads

Download demo project - 20 Kb
Download source - 5 Kb


Comments

  • anotherbugfix

    Posted by anotherbugfix on 11/15/2012 10:17am

    Yes, there are some old C++ programs out there still. If you wish to use this now (2012), you will also need to declare TotalColumnWidth outside of the for loop on line 66 like so: UINT TotalColumnWidth = 0; for (UINT Index = 0, TotalColumnWidth = LEFTMARGIN_OF_DROPDOWNLIST + RIGHTMARGIN_OF_DROPDOWNLIST; Index

    Reply
  • bug fix

    Posted by sunfy123 on 02/05/2008 02:00am

    in MultiColumnComboBox.cpp, line 213 ("if (DropDownWindowPointer != this) //Draw items in drop down list") DropDownWindowPointer might be NULL and generate an error. changing to "if ((DropDownWindowPointer != this) && (DropDownWindowPointer!=NULL) )" solves the problem

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

Top White Papers and Webcasts

  • Live Event Date: May 11, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT One of the languages that have always been supported with the Intel® RealSense™ SDK (Software Developer Kit) is JavaScript, specifically so that web-enabled apps could be created. Come hear from Intel Expert Bob Duffy as he reviews his own little "space shooting" game where the orientation of your face controls the aiming reticle to help teach developers how to write apps and games in JavaScript that can use facial and gesture …

  • Several technologies are driving big changes at federal agencies, but at the forefront is cloud computing. Cloud -- the use of remote, Internet-hosted servers for storing, managing and processing data in place of on-site technology -- is helping along technologies such as those related to collaboration and enabling new ones, such as the analysis of Big Data. Cloud is complicated but also growing in importance at federal agencies. Read this technology article to learn how governmental entities are creating a …

Most Popular Programming Stories

More for Developers

RSS Feeds

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