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



Comments

  • Simpler Solution!?

    Posted by Legacy on 05/04/1999 12:00am

    Originally posted by: Bryan C. Smith

    I too am building an app which requires dynamic listbox selection changing.  However, I did not go into the
    depth that was done here.  I simply handled the LBN_SELCHANGE message notification and put all my logic
    within it.  Here's some sample source:
    
    

    if (m_bSingleSelectMode)
    {
    int cSelItems = m_wndMSLB.GetSelCount();
    if (cSelItems > 1)
    {
    int nSkipItem = m_wndMSLB.GetCaretIndex();
    if (nSkipItem >= 0)
    {
    int* rgSelElems = new int[cSelItems];
    ASSERT(rgSelElems);
    m_wndMSLB.GetSelItems(cSelItems, rgSelElems);

    for (int i = 0; i < cSelItems; ++i)
    {
    m_wndMSLB.SetSel(rgSelElems[i], rgSelElems[i] == nSkipItem ? TRUE : FALSE);
    }

    delete[] rgSelElems; rgSelElems = NULL;
    }
    }
    }

    This seems to do the trick, however a little more thought would be necessary to allow only 2 selections at a time as another person posed in an earlier comment. I don't think it would be overly difficult though.

    Reply
  • Limit Multiple Listbox to 2 selections?

    Posted by Legacy on 01/19/1999 12:00am

    Originally posted by: Ryan Hayle

    I am trying to figure out if this is possible. I see I could use your implementation, and just unselect everything after the second selection, but there must be a better way.

    Thanks,
    Ryan Hayle
    <ryan@savvynet.com>

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Today's "average" business in general is ever more reliant on technology and the Internet. Mobility is the most often cited business trend that has transformed the way many of us work and communicate. From an IT security perspective, this means that protection methods and tools from even a few years ago are rapidly becoming "unfit for purpose." This guide provides crucial facts to assist you in building a robust business case, meeting the demands of your business, and protecting against threats now and in the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds