Button Click within a for each loop

Fairly new to VB express.

Is it possible to have a button click event from within a for each loop on a form i.e. the button being clicked refreshes the form and moves on to the next iteration of the loop?

Any suggestions welcomed.

[252 byte] By [ramjamman] at [2007-12-25]
# 1

yes you can but can you explain a bit more and perhaps some code you have?

to click a button programmatically you can do a button.PerformClick()

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

I am trying to create a form to add a row to a database in this row there are seven fields which id linked to another table. I wanted to create a combo box from which you can choose the item for each of these seven fields one after another from the one instance of the combo box and click a button to move onto the next rather than have seven combo boxes all displaying the same data.

May not be the best way to achieve the object but an interest in learning ways of doing things.

So to answer my question in basic terms if we look at

Dim myOtherArray() As String = {"Bob", "Beth", "Conrad", "Grant"}

Dim i As Integer = 0

For i = 0 To 3

Label1.Text = myOtherArray(i)

MessageBox.Show(myOtherArray(i))

Next

This will update Label1 on the main form each time the messagebox button is clicked

I wish to acheive the same effect on the main form by clicking the button on it rather than use the messagebox show button

ramjamman at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

just a suggestion, why don't you use a datagridview? with the datagridview you can add/delete/edit records, can bind to a dataset datasource and can automatically commit changes to a dataset, then you can use the dataAdapter to update the database.

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4

The answer to that one is it is not simplistic enough for the type of users I envisage.

I have experimented with both datagrid and details view and I am not satisfied with either for this particular case mainly because of the additional amount of related data that needs to be shown for each field it becomes confusing.

However if a button click cannot be called from within a loop I will need to think again and maybe use 7 identical forms to do the same thing.

ramjamman at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5

you can do a button click event (simulate a press of a button by mouse) - is this not what you are after? I posted the code in my previous post.

So are you wanting to press the same button everytime? or some other button?

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 6

Same button for each iteration through the loop

The form would have some text integrating labels a combo box with related data in labels and one button to confirm the entry and move on to the next step on the same form.

ramjamman at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 7

I hope this helps or sheds some light:

Dim myOtherArray() As String = {"Bob", "Beth", "Conrad", "Grant"}

Dim i As Integer = 0

For i = 0 To 3

Label1.Text = myOtherArray(i)

'MessageBox.Show(myOtherArray(i))

Me.someButton.PerformClick() 'this will click the button specified, replace "someButton" with the name of the button you want to click

Next

is this what you are after? my apologies for being slow!

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 8

is this what you are after? my apologies for being slow

No apologies needed it is so kind of you to support us newbies and probably answer the same questions over and over.

I did do a lot of research before posting a question and maybe what I want to do is not possible.

Unfortunately the Me.someButton.PerformClick() is the opposite to what I need

In simple language I would say I want to pause the loop and wait for the button click event by the user which will then continue the loop.

ramjamman at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 9
ah ok, thats clearer. Well to do this in an interation loop probably not. What you could do is store globally a "position" - to indicate what was the last position in your loop. So next time you continue after pressing the button for example, it would start back up from where it left off from (from the "position")...does this sound like something you would like?
ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 10
Yes indeed that does sound exactly what is needed.
ramjamman at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 11

ok, lets give it a shot.

At the top of your class, declare a variable:

..

..

public class MyClass

Dim thePosition as Integer = 0 'this is your global variable

'Declare your "myOtherArray" here also, so you can access it through out the class

Dim myOtherArray() as String = {"Bob", "Beth", "Conrad", "Grant"}

'methods start here

now this is the confusing part - still a bit blurry at this part. Now, do you want to just show the "myOtherArray" to the user, then wait for the user to click the button, which would then show the next item in the array? if so, then try this:

if Me.thePosition < myOtherArray.Length - 1 'check to see we have not exceeded the position in the array, otherwise an error will happen

Label1.Text = myOtherArray(Me.thePosition) 'Set the label to show the element in the array, from the last position set

MessageBox.Show(myOtherArray(Me.thePosition)) 'show it to the user, or you could just do MessageBox.Show(Label1.Text)

Me.thePosition = Me.thePosition + 1 'set the position ahead

end if

Does this help/make some sense? I'm probably confusing myself with some things...so please do correct me

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 12

Not exactly what I was wanting but it does give me enough to investigate further and it appears to be the answer I have been looking for.

Many thanks for your help and I will report back how I get on.

I have to go out now which is a drag when one is in the middle of something exciting.

Have a good day and thanks again.

ramjamman at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 13
no worries, glad I could help :-) Please do report back and give us more detail of exactly what you like and hopefully we can whip up a solution together
ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 14

Right I have achieved what I asked for all be it in a different fashion, the solution does what I was asking for.

The code is as you said works well

Public Class Form1

Dim myOtherArray() As String = {"Bob", "Beth", "Conrad", "Grant"}

Dim thePlace As Integer = 0 'this is your global variable

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If Me.thePlace < myOtherArray.Length Then

Label1.Text = myOtherArray(thePlace)

Me.thePlace = Me.thePlace + 1

End If

End Sub

End Class

Within your answer you have also explained the use of global variables within a class to me which I was fuzzy on.

Now the fun comes expanding this simple example to complete my objective.

I must thank you once again for your time and expertise as well as being so understanding.

Note for other readers all though this does not break/wait from within a loop for a button click, it creates it's own loop by iterating through the possible values of "thePlace" variable.

ramjamman at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...