ViRALiC
ViRALiC

Reputation: 1549

Multiple selections with ListBox?

I've decided to use a ListBox for my application, in this case, charitably named lb. I went on to fill it with content (only a handful of strings) and manipulated it's size and appearance before smacking it into my form.

Among the options I played with, I touched on lb.SelectionMode = SelectionMode.MultiSimple;, because I wanted the user to be able to select one or more of my strings.

Now, up until the point where I added the SelectionMode line, lb.SelectedIndex would always point me to the user's selection of choice (after it had been made, obviously).

But it doesn't seem to do so when multiple items are selected. It only seems to point to the "first" selected string.

Is there some way I can tell it to go to the NEXT selected item, or perhaps get a new list with the index of all the selected items?

How can I best go about getting all the selected strings my user chose?

Upvotes: 1

Views: 139

Answers (3)

NLindbom
NLindbom

Reputation: 508

Theres ListBox.SelectedIndices that returns a ListBox.SelectedIndexCollection, MSDN for reference and example.

Upvotes: 1

Kodre
Kodre

Reputation: 181

For getting collection of selected objects use property SelectedItems.

Upvotes: 0

Fredrik Mörk
Fredrik Mörk

Reputation: 158289

There is a plural version of the SelectedIndexproperty, called SelectedIndices. That is probably what your are looking for.

Also check the SelectedItems property, which may in fact be closer to what you actually want.

The above is true for Windows Forms application. If you are making a WPF application, SelectedItems is your friend.

Upvotes: 6

Related Questions