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
-
15 4247
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.
Question
How do i write a for next loop structure that has a counter that controls the time?
If that makes sense
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)?
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".......
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
-
thanks..i'll try and translate, thought i don't think i get it....
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.
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
-
-
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 - * ' 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.
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...
@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? - 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
-
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
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.
Thanks i figured it out..i create another private sub called animation in my program and i figured how to do the loops
Hurray! That's what we love here: people learning and figuring things out.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 =...
|
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...
|
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
|
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
|
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...
|
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...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |