ListBox multiple selection c#

❮ HTML tag

A drop-down list that allows multiple selections:

Choose a car:   Volvo   Saab   Opel   Audi

Try it Yourself »

Definition and Usage

The multiple attribute is a boolean attribute.

When present, it specifies that multiple options can be selected at once.

Selecting multiple options vary in different operating systems and browsers:

  • For windows: Hold down the control [ctrl] button to select multiple options
  • For Mac: Hold down the command button to select multiple options

Because of the different ways of doing this, and because you have to inform the user that multiple selection is available, it is more user-friendly to use checkboxes instead.

Browser Support

Attribute
multiple Yes Yes Yes Yes Yes

Syntax

❮ HTML tag

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 [416] 849-8900

You could use an ASP.NET-CustomValidator for this:

Providing a client-validation function:

function validateSelectionCount[sender, args]{ var listbox = document.getElementById['ListBox1']; args.IsValid = validateListBoxSelectionCount[listbox, 3, 3]; } function validateListBoxSelectionCount[listbox, minSelected, maxSelected]{ var selected=0; if[listbox != null]{ for [var i=0; imaxSelected]break; } } } return [selected >= minSelected && selected maxCount Then Exit For Next args.IsValid = [count >= minCount AndAlso count >“. You can also revoke the assigned area by selecting the area from the right and move it to the left List Box using the button “

Chủ Đề