FOR...NEXT LOOP in VB 2005

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mygirl22
    New Member
    • Feb 2009
    • 20

    FOR...NEXT LOOP in VB 2005

    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...=animat ion

    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


    Code:
    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
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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.

    Comment

    • mygirl22
      New Member
      • Feb 2009
      • 20

      #3
      Question
      How do i write a for next loop structure that has a counter that controls the time?
      If that makes sense

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        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)?

        Comment

        • mygirl22
          New Member
          • Feb 2009
          • 20

          #5
          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".......

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            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

            Code:
                        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

            Comment

            • mygirl22
              New Member
              • Feb 2009
              • 20

              #7
              thanks..i'll try and translate, thought i don't think i get it....

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                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.

                Comment

                • mygirl22
                  New Member
                  • Feb 2009
                  • 20

                  #9
                  translated

                  DO i have this right?

                  Code:
                      * 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

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    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
                    Code:
                      * ' 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.

                    Comment

                    • mygirl22
                      New Member
                      • Feb 2009
                      • 20

                      #11
                      the question revised

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

                      Comment

                      • mygirl22
                        New Member
                        • Feb 2009
                        • 20

                        #12
                        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?


                        Code:
                           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

                        Comment

                        • MrMancunian
                          Recognized Expert Contributor
                          • Jul 2008
                          • 569

                          #13
                          Code:
                          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

                          Comment

                          • tlhintoq
                            Recognized Expert Specialist
                            • Mar 2008
                            • 3532

                            #14
                            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.DoE vents() 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.

                            Comment

                            • mygirl22
                              New Member
                              • Feb 2009
                              • 20

                              #15
                              thanks

                              Thanks i figured it out..i create another private sub called animation in my program and i figured how to do the loops

                              Comment

                              Working...