Retrieving multiple ListBox selections

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbewers1
    New Member
    • Feb 2009
    • 68

    Retrieving multiple ListBox selections

    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.
    Code:
    foreach (ListItem li in fileList.Items)
    Code:
    for(int i = 0; i < fileList.Items.Count; i++)
    In both cases, I've ensured to use:
    Code:
    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
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    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.

    Code:
    foreach (object o in listBox1.SelectedItems) { ... }

    Comment

    Working...