Multiple/Single Selection ListBox Control | CodeGuru

Multiple/Single Selection ListBox Control

I find it quite irritating that one cannot change the style of a CListBox from single to multiple and back dynamically. After plenty of searching the most common solution I found was to place two ListBox controls, one set to Single selection and the other to multiple. The user then populates the two with the […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 24, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

I find it quite irritating that one cannot change the style of a CListBox from single to multiple and back dynamically.
After plenty of searching the most common solution I found was to place two ListBox controls, one set to Single selection
and the other to multiple. The user then populates the two with the same data and does the hide and show dance as
required. This is too frustrating.

The SRListBox class is implemented by taking a multiple selection listbox that can be made to behave like a single
select listbox. In the single select mode I simply unselect any other selections made. The SRListBox provides
the user with a function ( SetSingleSelect(const bool& single) ) to toggle between single and multiple selection.

In addition to this simple implementation I added and array of class SRCBAndLBItemArray. This class simply holds
pointers to all items currently selected. This saves me having to go through the painful routine of repeating the
code below each time I need my selections.

	int numberSelected = GetSelCount();
	if (numberSelected>0)
	{
		int *selIndex = new int[numberSelected];
		GetSelItems( numberSelected, selIndex );
		for (int i=0; i < numberSelected; i++)
		{
			blah....
		}
	}

The SRCBAndLBItemArray gets populated each time the SRListBox::UpdateData()
function gets called (perhaps I should have given this function another name).
I call this function from the DoDataExchange(CDataExchange* pDX) function of
parent window and from the OnSelectionChange message handler. (this is key,
without it the SRCBAndLBItemArray does not get populated). Perhaps someone
can suggest a better mechanism.

The CListBoxExDlg::OnSelchangeList1() function in the sample code displays
the use of the SRListBox data retrieval.

Download demo project – 27 KB

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.