Retrieving selectedvalue in multiselect listbox problem

  • Thread starter Thread starter IMK
  • Start date Start date
I

IMK

Hello all,
Sorry if this issue has come up already but I am new to vb.net.
Thanks.

I am trying to retrieve the selectedvalues from a multiselect list box
in a vb 2005 winform. Here is the code I am using so far:

dim i as object

For Each i In Me.lstUnitPicklist.SelectedItems
MessageBox.Show(Me.lstUnitPicklist.SelectedValue)
Next

Eventually I will replace the messagebox with the name of a sub that
will take the selectedvalue as a parameter.

The problem is that, as the code goes through each selected item, it
returns only the first selected value repeatedly for the number of
selected items.
In other words, If the listbox was a listbox of colors and I had
selected "blue", then "green" then "yellow" the code would show
"blue", "blue", "blue" then stop.

I have used similar code before and it worked fine. Is there another
way to do this? It seems so simple, but I am stumped.
 
D'oh

I must remember to use Google's wonderful "Search this group"
functionality
before I post. I found this from MVP Luke Zhang from several years
ago:

Here is the sample code for a work around:
Dim dview As DataRowView
For Each dview In ListBox1.SelectedItems
MsgBox(dview.Row.Item(0).ToString)
Next

That's all I needed. Thanks MVPs
 
Back
Top