Iterating through a ListBox in VB.NET VS 2005

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
 
P

Patrick Steele

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?
 
H

Henry Jones

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.
 
S

Stephany Young

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))
 
G

Guest

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.
 
S

Stephany Young

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top