Stop loop with button control

Ghoztrider

New member
Joined
Dec 5, 2010
Messages
1
Location
New England
Programming Experience
Beginner
Hi Folks,

I am fairly new to VB but really enjoying programming. I'm looking to create a counter that counts to 25 and loops back around again with a button control and another button control to stop it. So I press a button to start and another to stop and I want it to display the number it stopped on. Is this possible? and how do I do it if it is? I can figure out how to display the label and create a simple counter(that doesn't loop), but I'm unsure on the rest.
 
Here's the simplest way to do it, with a single thread:

1. On the first Button click, declare a counter variable and start a Do loop.
2. In the Do loop, increment the counter and then use the Mod operator to wrap back to 0 when you reach 25.
3. Call Application.DoEvents, which gives the app a chance to respond to user input, e.g. a Button click.
4. On the second Button click, set a Boolean variable to indicate that the loop should stop.
5. Back in the loop, check that flag and break out of the loop if it is set.
6. Display the final counter value to the user.

A more professional way would be to use multi-threading. That may be a bit advanced for you at the moment but, if you get this way working and would like to try it, post back and I can provide instructions.
 
How about using a Timer? For start button call the timers Start method, for stop button call its Stop method. For timers Tick event update (add or reset) and display a counter variable.
 
Back
Top