Connect with Expertise | Find Experts, Get Answers, Share Insights

Retrieving multiple ListBox selections

 
Join Date: Feb 2009
Location: Bath, UK
Posts: 65
#1: Mar 21 '10
Hi, I figure this has been asked somewhere before but, I can't retrieve the help I'm looking for.

Basically, I'm trying to parse the strings of all selected items in a ListBox called fileList.

I've tried this with a for loop as well as a foreach loop.
Expand|Select|Wrap|Line Numbers
  1. foreach (ListItem li in fileList.Items)
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < fileList.Items.Count; i++) 
In both cases, I've ensured to use:
Expand|Select|Wrap|Line Numbers
  1. fileList.SelectedItem.Selected
when looking for the string of the current index.
The rest of the method selects different methods from other classes and goes into more detail than we need here, so have omitted this.

Unfortunately, when I select multiple items, I get the first selected item posted each time, regardless of how many more items are selected.

What am I doing wrong?

GaryTexmo's Avatar
E
C
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 687
#2: Mar 21 '10

re: Retrieving multiple ListBox selections


The ListBox object keeps track of the items that are selected in the SelectedItems property. You should be using that to see what's selected.

Expand|Select|Wrap|Line Numbers
  1. foreach (object o in listBox1.SelectedItems) { ... }
Reply

Tags
listbox selecting