Connecting Tech Pros Worldwide Help | Site Map

FOR...NEXT LOOP in VB 2005

Newbie
 
Join Date: Feb 2009
Posts: 20
#1: Mar 20 '09
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


Expand|Select|Wrap|Line Numbers
  1. Private Sub ForNextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xForNextButton.Click, xTimer.Tick
  2.         Dim MyTime As Integer = 5
  3.         Counter = 1000
  4.  
  5.         For Counter = 0 To 1000
  6.             xTimer.Enabled = MyTime * 1
  7.             ' Move the button for a set amount of time
  8.             ' Exit the loop
  9.  
  10.         Next
  11.  
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#2: Mar 20 '09

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
#3: Mar 20 '09

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
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#4: Mar 20 '09

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
#5: Mar 20 '09

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".......
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#6: Mar 20 '09

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

Expand|Select|Wrap|Line Numbers
  1.             DateTime TimeOut = DateTime.Now.AddSeconds(30);// Use a variable for the 30 so as to make it more dynamic
  2.             while (DateTime.Now <= TimeOut)
  3.             {
  4.                 // Move the button one step
  5.                 Application.DoEvents(); // so the application isn't hung up
  6.             }
  7.             // This is what happens with the time is expired
  8.  
Newbie
 
Join Date: Feb 2009
Posts: 20
#7: Mar 20 '09

re: FOR...NEXT LOOP in VB 2005


thanks..i'll try and translate, thought i don't think i get it....
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#8: Mar 20 '09

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
#9: Mar 20 '09

re: FOR...NEXT LOOP in VB 2005


DO i have this right?

Expand|Select|Wrap|Line Numbers
  1.  
  2.     * Dim TimeOut As DateTime = DateTime.Now.AddSeconds(30)
  3.     * ' Use a variable for the 30 so as to make it more dynamic
  4.     * While DateTime.Now <= TimeOut
  5.     *     ' Move the button one step
  6.     *     Application.DoEvents()
  7.     *     ' so the application isn't hung up
  8.     * End While
  9.     * ' This is what happens with the time is expired
  10.     * If True Then
  11.     *     Dim i As Integer = 0, j As Integer = 1
  12.     *     While test(i, j)
  13.     *         printf("for loop i=%d" & vbLf, i)
  14.     *         increment(i, j)
  15.     *     End While
  16.     *     printf("Finished" & vbLf)
  17.     * End If
  18.  
  19.  
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#10: Mar 20 '09

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:
Expand|Select|Wrap|Line Numbers
  1.   * ' This is what happens with the time is expired
  2.     * If True Then
  3.     *     Dim i As Integer = 0, j As Integer = 1
  4.     *     While test(i, j)
  5.     *         printf("for loop i=%d" & vbLf, i)
  6.     *         increment(i, j)
  7.     *     End While
  8.     *     printf("Finished" & vbLf)
  9.     * 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
#11: Mar 22 '09

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
#12: Mar 22 '09

re: FOR...NEXT LOOP in VB 2005


Quote:

Originally Posted by mygirl22 View Post

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?


Expand|Select|Wrap|Line Numbers
  1.    Private Sub ForNextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xForNextButton.Click, xTimer.Tick
  2.         'Me.xTimer.Enabled = False
  3.         'Me.xTimeStartButton.Click.enabled = False
  4.  
  5.         For i As Integer = 0 To 2
  6.  
  7.         xEnjoyButton.Location = New Point(i,i)
  8.  
  9.         'Threading.Thread.Sleep(2)
  10.  
  11.         Next 'i
  12.  
  13.  
  14.  
  15.         xButtonLabel.Text = "Button(" & xEnjoyButton.Location.X.ToString & "," & xEnjoyButton.Location.Y.ToString & ")"
  16.  
  17.  
  18.     End Sub
MrMancunian's Avatar
Expert
 
Join Date: Jul 2008
Location: Utrecht, The Netherlands
Posts: 274
#13: Mar 22 '09

re: FOR...NEXT LOOP in VB 2005


Expand|Select|Wrap|Line Numbers
  1. Threading.Thread.Sleep(2)
  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
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#14: Mar 22 '09

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
#15: Mar 26 '09

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
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#16: Mar 26 '09

re: FOR...NEXT LOOP in VB 2005


Hurray! That's what we love here: people learning and figuring things out.
Reply