Home All Groups Group Topic Archive Search About

Iterating through a ListBox in VB.NET VS 2005

Author
12 Oct 2006 11:09 PM
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

Author
12 Oct 2006 11:33 PM
Patrick Steele
In article <#dG2BOl7GHA.1***@TK2MSFTNGP04.phx.gbl>, he***@yada.com
says...
Show quoteHide quote
> 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?

Author
12 Oct 2006 11:40 PM
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.


Show quoteHide quote
"Patrick Steele" <patr***@mvps.org> wrote in message
news:MPG.1f989acbef6948db9896b0@msnews.microsoft.com...
> In article <#dG2BOl7GHA.1***@TK2MSFTNGP04.phx.gbl>, he***@yada.com
> says...
>> 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?
>
> --
> Patrick Steele
> http://weblogs.asp.net/psteele
Author
12 Oct 2006 11:50 PM
Kerry Moorman
Henry,

This works for me in VS2003:

for each itm as DataRowView in objlistbox.selecteditems

     console.writeline(itm("ContactName"))

next

Kerry Moorman


Show quoteHide quote
"Henry Jones" wrote:

> 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.
>
>
> "Patrick Steele" <patr***@mvps.org> wrote in message
> news:MPG.1f989acbef6948db9896b0@msnews.microsoft.com...
> > In article <#dG2BOl7GHA.1***@TK2MSFTNGP04.phx.gbl>, he***@yada.com
> > says...
> >> 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?
> >
> > --
> > Patrick Steele
> > http://weblogs.asp.net/psteele
>
>
>
Author
12 Oct 2006 11:47 PM
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))


Show quoteHide quote
"Henry Jones" <he***@yada.com> wrote in message
news:%23dG2BOl7GHA.1256@TK2MSFTNGP04.phx.gbl...
>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
>
>
Author
12 Oct 2006 11:55 PM
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.


Show quoteHide quote
"Henry Jones" <he***@yada.com> wrote in message
news:%23dG2BOl7GHA.1256@TK2MSFTNGP04.phx.gbl...
>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
>
>
Author
13 Oct 2006 2:32 PM
Henry Jones
Thanks for the code to iterate through the listbox.  Your suggestions
worked!

H


Show quoteHide quote
"Henry Jones" <he***@yada.com> wrote in message
news:%23dG2BOl7GHA.1256@TK2MSFTNGP04.phx.gbl...
>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
>
>