473,409 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

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...=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.  
Mar 20 '09 #1
15 4247
tlhintoq
3,525 Expert 2GB
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.
Mar 20 '09 #2
Question
How do i write a for next loop structure that has a counter that controls the time?
If that makes sense
Mar 20 '09 #3
tlhintoq
3,525 Expert 2GB
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)?
Mar 20 '09 #4
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".......
Mar 20 '09 #5
tlhintoq
3,525 Expert 2GB
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.  
Mar 20 '09 #6
thanks..i'll try and translate, thought i don't think i get it....
Mar 20 '09 #7
tlhintoq
3,525 Expert 2GB
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.
Mar 20 '09 #8
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.  
Mar 20 '09 #9
tlhintoq
3,525 Expert 2GB
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
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.
Mar 20 '09 #10
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...
Mar 22 '09 #11
@mygirl22
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
Mar 22 '09 #12
MrMancunian
569 Expert 512MB
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
Mar 22 '09 #13
tlhintoq
3,525 Expert 2GB
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.
Mar 22 '09 #14
Thanks i figured it out..i create another private sub called animation in my program and i figured how to do the loops
Mar 26 '09 #15
tlhintoq
3,525 Expert 2GB
Hurray! That's what we love here: people learning and figuring things out.
Mar 26 '09 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: nasht | last post by:
Hi, Interesting question: A user fills a form about how often an event happen. He has 4 fields: . In any of them, he can enter "*" as a joker, to say it doesn't matter. For example, his...
2
by: Patrik | last post by:
Hi, I cannot find the right way to write this code. I keep getting the error : Next without For Here's my code. For Each Choix In RapInd_Code.ItemsSelected S6 =...
8
by: Terry Olsen | last post by:
How do I loop back to the beginning of a for/next loop before getting to the end of it? Isn't there an "iterate" command or something like that? For Each This in That ...code if This = False...
23
by: Mitchell Vincent | last post by:
Is there any way to "skip" iterations in a for loop? Example : for x = 1 to 10 if something = 1 next endif
13
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
4
by: Neo | last post by:
I found on error resume next doesn't work in for each... e.g. on error resume next for each x in y 'do stuff next if you have an error in for each loop, it falls in infinite loop... it...
6
by: Lucia | last post by:
Hi, everyone, I have a for statement in my programm. I want to know if there is any way to goto next i when some error occurs. I tried with the following code. But it doesn't work... for i=0...
0
ADezii
by: ADezii | last post by:
If you want to visit each item in an Array, you have two alternatives: Use a For Each..Next loop, using a Variant to retrieve each value in turn. Use a For...Next loop, looping from the Lower...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.