By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,276 Members | 1,406 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,276 IT Pros & Developers. It's quick & easy.

loop through listbox values

P: n/a
yue
Hi,

How do I loop through listbox items?

Thanks,

yue
Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
This will serve.

foreach(object o in listBox1.Items)

Sam
"yue" <yu*@mail.com> wrote in message
news:0c****************************@phx.gbl...
Hi,

How do I loop through listbox items?

Thanks,

yue

Nov 15 '05 #2

P: n/a
yue
Hi,

That did not work also.

yue
-----Original Message-----
try:

foreach (string s in listBox1.Items)

"yue" <yu*@hotmail.com> wrote in message
news:4d****************************@phx.gbl...
Hi Sam,
i have done this but cant get the row values.

Please help,

thanks,

yue
>-----Original Message-----
>This will serve.
>
> foreach(object o in listBox1.Items)
>
>Sam
>
>
>"yue" <yu*@mail.com> wrote in message
>news:0c****************************@phx.gbl...
>> Hi,
>>
>> How do I loop through listbox items?
>>
>> Thanks,
>>
>> yue
>
>
>.
>

.

Nov 15 '05 #3

P: n/a
ListBox items accept objects of any type as elements. Find where the
elements are being added, and use that class in the foreach. For instance,
if you do this

listBox1.Items.Add(new MyCustomClass("Hi", 3, true));

then you would do this to get the values

foreach (MyCustomClass item in listBox1.Items) {
}

Chris

"yue" <yu*@mail.com> wrote in message
news:0c****************************@phx.gbl...
Hi,

How do I loop through listbox items?

Thanks,

yue

Nov 15 '05 #4

P: n/a
yue
Chris,
This is good if you use the add method of the listbox.
I set the listbox Datasource on a filtered DataTable.
This is why i need to loop through the listbox and get the
elements.

Is there no easy way to do this? This is so frustrating to
me...

yue
-----Original Message-----
ListBox items accept objects of any type as elements. Find where theelements are being added, and use that class in the foreach. For instance,if you do this

listBox1.Items.Add(new MyCustomClass("Hi", 3, true));

then you would do this to get the values

foreach (MyCustomClass item in listBox1.Items) {
}

Chris

"yue" <yu*@mail.com> wrote in message
news:0c****************************@phx.gbl...
Hi,

How do I loop through listbox items?

Thanks,

yue

.

Nov 15 '05 #5

P: n/a
In this case, you ought to loop through the datasource instead of the list
box's collection. For a DataTable, this is

foreach (DataRow row in dataTable1.Rows) {}

It ought to be similar for a DataView.

Chris

"yue" <yu*@hotmail.com> wrote in message
news:4e****************************@phx.gbl...
Chris,
This is good if you use the add method of the listbox.
I set the listbox Datasource on a filtered DataTable.
This is why i need to loop through the listbox and get the
elements.

Is there no easy way to do this? This is so frustrating to
me...

yue
-----Original Message-----
ListBox items accept objects of any type as elements.

Find where the
elements are being added, and use that class in the

foreach. For instance,
if you do this

listBox1.Items.Add(new MyCustomClass("Hi", 3, true));

then you would do this to get the values

foreach (MyCustomClass item in listBox1.Items) {
}

Chris

"yue" <yu*@mail.com> wrote in message
news:0c****************************@phx.gbl...
Hi,

How do I loop through listbox items?

Thanks,

yue

.

Nov 15 '05 #6

P: n/a
yue
Chris,

Thank You for all your help.

I will try this.

yue
-----Original Message-----
In this case, you ought to loop through the datasource instead of the listbox's collection. For a DataTable, this is

foreach (DataRow row in dataTable1.Rows) {}

It ought to be similar for a DataView.

Chris

"yue" <yu*@hotmail.com> wrote in message
news:4e****************************@phx.gbl...
Chris,
This is good if you use the add method of the listbox.
I set the listbox Datasource on a filtered DataTable.
This is why i need to loop through the listbox and get the elements.

Is there no easy way to do this? This is so frustrating to me...

yue
>-----Original Message-----
>ListBox items accept objects of any type as elements.

Find where the
>elements are being added, and use that class in the

foreach. For instance,
>if you do this
>
>listBox1.Items.Add(new MyCustomClass("Hi", 3, true));
>
>then you would do this to get the values
>
>foreach (MyCustomClass item in listBox1.Items) {
>}
>
>Chris
>
>"yue" <yu*@mail.com> wrote in message
>news:0c****************************@phx.gbl...
>> Hi,
>>
>> How do I loop through listbox items?
>>
>> Thanks,
>>
>> yue
>
>
>.
>

.

Nov 15 '05 #7

P: n/a
On Wed, 30 Jul 2003 08:22:31 -0700, in the
microsoft.public.dotnet.languages.csharp group, yue said...
How do I loop through listbox items?


Items is a property of a listBox containing a collection. If you follow
the links from ListBox Class to ListBox Members to Items (inherited from
ListControl) to ListControl.Items Property you will find one example of
how to iterate through the list.

In addition, follow through from CollectionBaseClass, CollectionBase
Members to GetEnumerator which describes also MoveNext, Current and
Reset.

So essentially you have choices of for, foreach and GetEnumerator
approaches to do what you want to do.

Gerald
Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.