FOR...NEXT LOOP in VB 2005 | Newbie | | Join Date: Feb 2009
Posts: 20
| |
1.I have created a moving object (which is a button) in a form (rectangle box) called "Enjoy Button" and it moves when
2. a specific button named "Start Timer" is pressed...When the "Start Timer" button is pressed, the
3. "xTimer" event is called and the button moves in a rectangle box and bounces of the walls...=animation
4. However, i need to write a For..Next Loop in the
5."For Next " Button= a button named for next
6.and also put a counter in the loop to control the animation time..
7.The loop should stop at a certain time, and the object too should stop at that time,at a new location
this is what i have so far - Private Sub ForNextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xForNextButton.Click, xTimer.Tick
-
Dim MyTime As Integer = 5
-
Counter = 1000
-
-
For Counter = 0 To 1000
-
xTimer.Enabled = MyTime * 1
-
' Move the button for a set amount of time
-
' Exit the loop
-
-
Next
-
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: FOR...NEXT LOOP in VB 2005
My compliments on how well you have described what your goals are and your intended plan of action to get there. Not everyone does that as well.
But... What is your question for the volunteers here?
By the way... Line 6 is probably going to give you problems. .Enabled is generally a boolean (true/false) but you are repeatedly setting it equal to 5*1.
| | Newbie | | Join Date: Feb 2009
Posts: 20
| | | re: FOR...NEXT LOOP in VB 2005
Question
How do i write a for next loop structure that has a counter that controls the time?
If that makes sense
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: FOR...NEXT LOOP in VB 2005
Ah... To control time... Wouldn't that just be wonderful? I would never have to worry about a deadline again. <laugh>
First lets decide which of the two things you're talking about is the important one: Do you want this to work based on a counter; where the animation will take place for a set number of times (number of moves)... or do you want the animation to take place for a given amount of elapsed time (30 seconds)?
| | Newbie | | Join Date: Feb 2009
Posts: 20
| | | re: FOR...NEXT LOOP in VB 2005
you are too funny..lol
I want the counter to control the animation time..when the button named " For Next..."is clicked.....say maybe 30 seconds, then the loop should stop without clicking the "For..NExt Button again".......
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: FOR...NEXT LOOP in VB 2005
What I'm getting at is that a counter is not necessarily where you are going with this. A loop yes, but it doesn't have to be a loop based on counting.
It sounds like you want a loop based on time: The loop continues until the time runs out.
I can give you a sample in C#. It shouldn't be too rough to follow the logic and translate to VB - DateTime TimeOut = DateTime.Now.AddSeconds(30);// Use a variable for the 30 so as to make it more dynamic
-
while (DateTime.Now <= TimeOut)
-
{
-
// Move the button one step
-
Application.DoEvents(); // so the application isn't hung up
-
}
-
// This is what happens with the time is expired
-
| | Newbie | | Join Date: Feb 2009
Posts: 20
| | | re: FOR...NEXT LOOP in VB 2005
thanks..i'll try and translate, thought i don't think i get it....
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: FOR...NEXT LOOP in VB 2005
The idea is you needed a 'loop' but not necessarily a 'counter', if I understood the goal.
That's exactly what a 'while' loop is. As long as the conditions within the ( ) are true, the body { } will keep looping.
So we set a DateTime object to the current time, plus however long we want to keep going: Our TimeOut/ At 12:30:10 the timeout becomes 12:30:40 in this example.
As the loop runs it keeps comparing the the current time to the timeout time.
First run through loop 12:30:11 is less than or equal to 12:30:40 so the loop runs... at 12:30:12 it is still true; at 12:30:39 its true;... at :12:30:41 the test condition is no longer true, so the While loop ends.
| | Newbie | | Join Date: Feb 2009
Posts: 20
| | | re: FOR...NEXT LOOP in VB 2005
DO i have this right? -
-
* Dim TimeOut As DateTime = DateTime.Now.AddSeconds(30)
-
* ' Use a variable for the 30 so as to make it more dynamic
-
* While DateTime.Now <= TimeOut
-
* ' Move the button one step
-
* Application.DoEvents()
-
* ' so the application isn't hung up
-
* End While
-
* ' This is what happens with the time is expired
-
* If True Then
-
* Dim i As Integer = 0, j As Integer = 1
-
* While test(i, j)
-
* printf("for loop i=%d" & vbLf, i)
-
* increment(i, j)
-
* End While
-
* printf("Finished" & vbLf)
-
* End If
-
-
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: FOR...NEXT LOOP in VB 2005
I can't completely vouch for the translation as am I a C# guy and just read VB for the "gist of it", but that looks about right.
I don't completely follow the part you have after the loop is done moving the button around Quote: - * ' This is what happens with the time is expired
-
* If True Then
-
* Dim i As Integer = 0, j As Integer = 1
-
* While test(i, j)
-
* printf("for loop i=%d" & vbLf, i)
-
* increment(i, j)
-
* End While
-
* printf("Finished" & vbLf)
-
* End If
if True ... No comparison taking place here so it will always be true
I don't know what your test(int i, int j) method does, so I have no idea under what conditions it will return true or false. Therefore its hard to say if this loop will ever execute.
| | Newbie | | Join Date: Feb 2009
Posts: 20
| | | re: FOR...NEXT LOOP in VB 2005
Oky, i finally got the directions right...
Hi,
I wanted to know how to create a "for...next loop" to up date an animated ball location? the ball moves in a form and bounces off four walls...the "for...next loop" will be activated when the "for..next button" is clicked...then the ball will move to another location.....it has nothing to do with time...
| | Newbie | | Join Date: Feb 2009
Posts: 20
| | | re: FOR...NEXT LOOP in VB 2005 Quote:
Originally Posted by mygirl22 Oky, i finally got the directions right...
Hi,
I wanted to know how to create a "for...next loop" to up date an animated ball location? the ball moves in a form and bounces off four walls...the "for...next loop" will be activated when the "for..next button" is clicked...then the ball will move to another location.....it has nothing to do with time... this is what i have so far but the problem is that the button (animation) is not moving five times before it stop and it is moving too fast..any ideas anyone? - Private Sub ForNextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xForNextButton.Click, xTimer.Tick
-
'Me.xTimer.Enabled = False
-
'Me.xTimeStartButton.Click.enabled = False
-
-
For i As Integer = 0 To 2
-
-
xEnjoyButton.Location = New Point(i,i)
-
-
'Threading.Thread.Sleep(2)
-
-
Next 'i
-
-
-
-
xButtonLabel.Text = "Button(" & xEnjoyButton.Location.X.ToString & "," & xEnjoyButton.Location.Y.ToString & ")"
-
-
-
End Sub
|  | Expert | | Join Date: Jul 2008 Location: Utrecht, The Netherlands
Posts: 274
| | | re: FOR...NEXT LOOP in VB 2005 -
Threading.Thread.Sleep(2)
-
This means that it will sleep 2 miliseconds every loop. 1000 miliseconds is 1 second, so you should change the sleepvalue if you want something to move at an acceptable speed.
Steven
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: FOR...NEXT LOOP in VB 2005
Also going from location (0,0) to (1,1) to (2,2) may not look like movement.
It may of moved diagonally 2 pixels but that's such a small movement that its hard to detect.
I suggest putting in the Application.DoEvents() from the earlier posts so you give the form some processor time to update the display. Often these types of things will do as they are told, the button will move, but the form doesn't have a chance to OnPaint() the updates.
| | Newbie | | Join Date: Feb 2009
Posts: 20
| | | re: FOR...NEXT LOOP in VB 2005
Thanks i figured it out..i create another private sub called animation in my program and i figured how to do the loops
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: FOR...NEXT LOOP in VB 2005
Hurray! That's what we love here: people learning and figuring things out.
|  | Similar Visual Basic .NET bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|