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 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++)

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

More by Author

Must Read