For each loop for listbox items?

K

kimiraikkonen

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

Lloyd Sheen

kimiraikkonen said:
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
 
T

Tom Shelton

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
 
K

kimiraikkonen

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

kimiraikkonen

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

Lloyd Sheen

kimiraikkonen said:
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
 

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