473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Iterating through a ListBox in VB.NET VS 2005

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
Oct 12 '06 #1
6 1309
In article <#d**************@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
Oct 12 '06 #2
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" <pa*****@mvps.orgwrote in message
news:MP************************@msnews.microsoft.c om...
In article <#d**************@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

Oct 12 '06 #3
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 Jones" <he***@yada.comwrote in message
news:%2****************@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


Oct 12 '06 #4
Henry,

This works for me in VS2003:

for each itm as DataRowView in objlistbox.selecteditems

console.writeline(itm("ContactName"))

next

Kerry Moorman
"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" <pa*****@mvps.orgwrote in message
news:MP************************@msnews.microsoft.c om...
In article <#d**************@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


Oct 12 '06 #5
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.SelectedIndi cies(_i))("ContactName"))
Next

Each item in SelectedIndicies effectively gives you the row number in the
source.
"Henry Jones" <he***@yada.comwrote in message
news:%2****************@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


Oct 12 '06 #6
Thanks for the code to iterate through the listbox. Your suggestions
worked!

H
"Henry Jones" <he***@yada.comwrote in message
news:%2****************@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


Oct 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lise | last post by:
Hi all .. I have a directory with several subdirectories and a number of files in each. These files are accessed by going to a web page and clicking in a listbox and navigating to the desired...
4
by: Jason | last post by:
Here is an odd issue. I am trying to shed some light on why this is causing a problem. I have an ArrayList. I am binding it to a ListBox control with has its Sort property set to True. If the...
7
by: GTi | last post by:
In Win32 I have a function that opens several Dialogs and put the window handle in a listbox (together with a string): HWND hWnd = CreateDialogParam(...) LB_SETITEMDATA, index, (LPARAM)hWnd); ...
3
by: Raja | last post by:
I have a simple question, I have a datagrid and inside the grid, i have List box. I am able to render the page with the datagrid and the lisbox values. Now, my question is how to trap the server...
5
by: Dave H | last post by:
I have an asp:listbox, allowing multiple selections, is there a quick check to see if there's more than one selected, or do I need to go through the whole list? Thanks, Dave
3
by: Hugh O | last post by:
Hi, There is a ROW property on the Web Listbox control within VB.Net. The description states it is "The number of visible rows to display". I have tried setting this property in many different...
7
by: Paul Bromley | last post by:
How can I use this please - I need 2 columns. I have been having difficulty finding info on this and the 2005 Treeview control today. Many thanks for any links or info, Paul Bromley
1
by: Crazy Cat | last post by:
Hi, I have a form with a databound listbox and combobox. The listbox items are filtered based on the selection for the combobox. If I click on the listbox to select an item, I can no longer...
1
by: lance2001 | last post by:
Hi, After viewing the entire Visual Basic 2005 Express Edition for Beginners video series, I have begun building a database driven application that will make use of listbox1 (multi-select)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.