Iterating through a ListBox in VB.NET VS 2005

  • Thread starter Thread starter Henry Jones
  • Start date Start date
H

Henry Jones

I have a listbox on a Windows form and it is bound using the following code:

objListBox.DataSource = tbl

objListBox.DisplayMember = "ContactName"

objListBox.ValueMember = "ContactID"

------------------------------------

On my form, I have a multi-select listbox and I would like to select a few
items. So If my listbox contains: CA, NV, OR, WA, AZ

and I choose CA and NV,



How can I get get those items?

I tried something like

dim itm as object

for each itm in objlistbox.selecteditems

console.writeline(itm)

next



but it doesn't work. Why is this so hard to get the selected items in a
listbox?



Thanks
 
I have a listbox on a Windows form and it is bound using the following code:

objListBox.DataSource = tbl

objListBox.DisplayMember = "ContactName"

objListBox.ValueMember = "ContactID"

------------------------------------

On my form, I have a multi-select listbox and I would like to select a few
items. So If my listbox contains: CA, NV, OR, WA, AZ

and I choose CA and NV,



How can I get get those items?

I tried something like

dim itm as object

for each itm in objlistbox.selecteditems

console.writeline(itm)

next



but it doesn't work.

What do you mean "it doesn't work"? Do you get an error?
 
I get the following error:
Argument 'Prompt' cannot be converted to type 'String'.

but I have tried other things and they don't work also.

For example I tried console.writeline(objlist.selecteditem) and tried to
list objlist.text and many other things but just can't display the State or
it's value.

If I could see some working code on how to do this that would be great. I'm
not necessarily in need of fixing what I have, I would like to get an
example of something that someone already has that works.

Thanks.
 
You are on the right track, but you have to remember that the SelectedItems
collection is a collection of ListItems.

Each selected item is a ListItem that contains a string and therfore you
need to 'turn' the ListItem into a string before you can display the correct
value.

console.writeline(ctype(itm, string))
 
Henry,

This works for me in VS2003:

for each itm as DataRowView in objlistbox.selecteditems

console.writeline(itm("ContactName"))

next

Kerry Moorman


Henry Jones said:
I get the following error:
Argument 'Prompt' cannot be converted to type 'String'.

but I have tried other things and they don't work also.

For example I tried console.writeline(objlist.selecteditem) and tried to
list objlist.text and many other things but just can't display the State or
it's value.

If I could see some working code on how to do this that would be great. I'm
not necessarily in need of fixing what I have, I would like to get an
example of something that someone already has that works.

Thanks.
 
Sorry, I didn't read you post properly.

Because your ListBox is bound, each item is a reference to a row in tbl.

In this case you need to use SelectedIndicies instead of SelectedItems and
retrieve the value from tbl:

For _i as Integer = 0 to objlistbox.SelectedIndicies.Count
Console.WriteLine(tbl.Rows(objlistbox.SelectedIndicies(_i))("ContactName"))
Next

Each item in SelectedIndicies effectively gives you the row number in the
source.
 
Back
Top