TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > VB.NET

For each loop for listbox items?

VB.NET

 
 
 
Thread Tools Display Modes
Old 10-11-2007, 03:03 PM   #1
kimiraikkonen
Guest
 
Posts: n/a
For each loop for listbox items?

Hi,
I'm trying to do a batch process for all the items in my listbox1
using "for each...next" ? I'm not well-experienced on this yet, how
can i sample this?

For example: From item1 to last item (there's no guess for how many
items a user can add as well),
I want my external process run one by one. Process must run for item1
then item2 when item1's work finishes....until the last listbox item.

How can i do that?


Thanks.

 
Old 10-11-2007, 04:04 PM   #2
Lloyd Sheen
Guest
 
Posts: n/a
Re: For each loop for listbox items?


"kimiraikkonen" <kimiraikkonen85***********> wrote in message
news:1194708691.825152.15950@v2g2000hsf.googlegrou ps.com...
> Hi,
> I'm trying to do a batch process for all the items in my listbox1
> using "for each...next" ? I'm not well-experienced on this yet, how
> can i sample this?
>
> For example: From item1 to last item (there's no guess for how many
> items a user can add as well),
> I want my external process run one by one. Process must run for item1
> then item2 when item1's work finishes....until the last listbox item.
>
> How can i do that?
>
>
> Thanks.
>


Each item in the listbox is an object and uses the tostring property to
display the item.

So you would do the following:

for each li as (your object type or string if you stored them that way) in
listbox1.items

' within the loop you will process each item.
' lets say you store the execute path of the process you want to start
so li would be a string

dim yourProcess as Process
yourProcess = Process.Start(li)
yourProcess.WaitForExit()

next

Has no error checking but I would not want you to lose out on some of the
fun.

Hope this helps
Lloyd Sheen

 
Old 10-11-2007, 04:04 PM   #3
Tom Shelton
Guest
 
Posts: n/a
Re: For each loop for listbox items?

On Nov 10, 8:31 am, kimiraikkonen <kimiraikkone...***********> wrote:
> Hi,
> I'm trying to do a batch process for all the items in my listbox1
> using "for each...next" ? I'm not well-experienced on this yet, how
> can i sample this?
>
> For example: From item1 to last item (there's no guess for how many
> items a user can add as well),
> I want my external process run one by one. Process must run for item1
> then item2 when item1's work finishes....until the last listbox item.
>
> How can i do that?
>
> Thanks.


A for each/next will work as long as you don't tend to actually add or
remove items from the list. Otherwise, you'll have to do a normal for
loop (though, you'll need to go backwards to avoid errors).

Anway here is a for each loop:

For Each item As MyItemType In MyComboBox.Items
' do cool stuff with item
Next

HTH

--
Tom Shelton

 
Old 10-11-2007, 04:04 PM   #4
kimiraikkonen
Guest
 
Posts: n/a
Re: For each loop for listbox items?

None of them worked

i got error for "next" saying:

"List that this enumerator is bound to has been modified. An
enumerator can only be used if the list does not change."

OK, i wanna rewind it, forget external process, let's go with
simple

Think a listbox1 has about 10 items (with different names, bla-bla-
bla...),
When i press button1, i want "each" item to be selected one by one for
all the items in the list, meanwhile the selected item's value must be
displayed in messagebox (e.g. msgbox(listbox1.selecteditem) )

Which proper loop function/syntax can i do with?

Very thanks...

 
Old 10-11-2007, 04:04 PM   #5
kimiraikkonen
Guest
 
Posts: n/a
Re: For each loop for listbox items?

I'm afraid and apologize, None of them worked or i couldn't do

i got error for "next" saying:

"List that this enumerator is bound to has been modified. An
enumerator can only be used if the list does not change."

OK, i wanna rewind it, forget external process, let's go with
simple

Think a listbox1 has about 10 items (with different names, bla-bla-
bla...),
When i press button1, i want "each" item to be selected one by one for
all the items in the list using Me.ListBox1.SelectedIndex =
Me.ListBox1.SelectedIndex + 1,
meanwhile the selected item's value must be
displayed in messagebox (e.g. msgbox(listbox1.selecteditem) )

Which proper loop function/syntax can i do with?

Very thanks...



 
Old 10-11-2007, 05:04 PM   #6
Lloyd Sheen
Guest
 
Posts: n/a
Re: For each loop for listbox items?


"kimiraikkonen" <kimiraikkonen85***********> wrote in message
news:1194713405.268676.257120@57g2000hsv.googlegro ups.com...
> I'm afraid and apologize, None of them worked or i couldn't do
>
> i got error for "next" saying:
>
> "List that this enumerator is bound to has been modified. An
> enumerator can only be used if the list does not change."
>
> OK, i wanna rewind it, forget external process, let's go with
> simple
>
> Think a listbox1 has about 10 items (with different names, bla-bla-
> bla...),
> When i press button1, i want "each" item to be selected one by one for
> all the items in the list using Me.ListBox1.SelectedIndex =
> Me.ListBox1.SelectedIndex + 1,
> meanwhile the selected item's value must be
> displayed in messagebox (e.g. msgbox(listbox1.selecteditem) )
>
> Which proper loop function/syntax can i do with?
>
> Very thanks...
>
>
>


If you remove an item from the list while in a for each loop you will get
that message. If you are processing all the items you can clear the listbox
after all the items are processed or do the following:

for i as integer = 0 to listbox1.items.count-1

next

LS

 
 

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 11:28 PM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional