Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I exit a loop with a button_click event

Status
Not open for further replies.

pmlapl

Technical User
May 20, 2010
4
US
I'm very much a novice to VB. Used to write Basic code years ago but VB is much more complicated. I want to start and stop a loop with a button click. One or two buttons will do. In basic I could call a subroutine in the loop and check for a stop. Tried a module in VB but wasn't successful. I used: If Button1_Click = true then End, but it didn't work. I'd appreciate a method on how to accomplish this.
Thanks in advance for the help.
 
I'm using VB6.0 syntax here which will convert almost directly to VBA, but may need some modification for .NET...
Code:
Option Explicit

Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

Dim StopLoop As Boolean 'false by default

Private Sub Command1_Click()
LoopIt
End Sub

Private Sub LoopIt()
Do While StopLoop = False
  Doevents
  Sleep 100 '1/10 of a second so cpu is not eaten up
Loop
End Sub

Private Sub Command2_Click()
StopLoop = True
End Sub

Many other variations can be derived from this simple example and I hope you get the gist of it...



Good Luck

 
Thanks much for your suggestion. I'm using VB 2008 and formatted for it, your version looks like this:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" _
(ByVal dwMilliseconds As Long)
Dim StopLoop As Boolean

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Do While StopLoop = False
Sleep(100)
Loop
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
StopLoop = True
End Sub
End Class

Unfortunately it doesn't work. It continues to run and won't stop because Button2_Click is outside Button1_Click's loop.

VB 2008 won't run a Button_Click event inside another.
I appreciate your help though.
Thanks again.
 
That is why the doevents inside the loop, to allow other events to execute. I belive in vb.net it would system.doevents...



Good Luck

 
I sincerely appreciate your efforts to help me, but my level of programming in VB 2008 is at the very beginning stages. I understand the basics of programming, just not in the language of VB 2008. Programmers communicating to programmers in the same language speak in esoteric terms common to the language they know. Others outside can't understand. I am on the outside. You appear to be knowlegeable in programming but not in VB 2008. "System" refers to a namespace, whatever that means. "System.DoEvents" doesn't exist in VB 2008 and even if it did, I wouldn't know how to use it in a program. I need someone to write an example of how to exit a loop with a button click in VB 2008 and explain each step. Perhaps I'm not at a level of understanding to make that request or to be here on this forum.

Once again, thanks for trying to help me.
 
It is 'Application.DoEvents' in VB2008

Some background:

1) Visual Basic (prior to the .NET versions, such as VB2008) was a single-threaded application. Without going in to the full complexity, this means that whilst your program is doing one thing it cannot also be doing another. So, if you are zipping around a loop you cannot do something else in your program at the same time - like respond to a button click and set a variable. DoEvents exists (amongst other things) to allow certain different part of your program to run. In particular it allows GUI events, such as button presses, to be responded to.

2) .NET versions of VB, such as VB2008 (I'll just say VB2008 from now on), were designed to be a bit like traditional VB in order to make the transition for VB programmers as painless as possible (the successof this strategy is arguable). As a result, typically VB2008 programs run in a single thread, just like tradional VB programs, and traditional way to get the GUI to be responsive still exists: DoEvents.

3) VB2008, however, is not really like traditional VB. It is builtr on the Common Language Runtime, and every command comes from a library (or 'namespace'). Many of the commands therefore have to be explicitly referenced using the name of the library/namespace (from now on I'll just say namespace) to ensure that the right one is used.

4) In VB2008 the DoEvents command is in the Application namespace, so to use it you need to be explicit as follows:

Application.DoEvents

So, here's a short VB2008 program illustrating the technique. You'll require a form and two buttons:
Code:
[blue]Public Class Form1
    Private EndLoop As Boolean = False ' EndLoop variable visible by all subs/functions in this module

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Loopy() ' start our never-ending loop
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        EndLoop = True ' Flag loop to stop
    End Sub

    Private Sub Loopy()
        Dim lp As Long
        Do Until EndLoop ' Check to see if we have flagged loop to stop
            lp = (lp + 1) Mod 100
            TextBox1.Text = lp
            Application.DoEvents() ' allow certain other threads to run by processing all Windows messages currently in the message queue - allows GUI events to occur
        Loop
        MsgBox("Loop successfully terminated")
    End Sub
End Class[/blue]
 
(but as vb5prgrmr suggest, this is probably better taken to forum796, since this forum is actually for traditional VB)
 
System... Application... I knew it was one of them :) (Don't do a whole lot of .net here lately and what little I did know is fading like a bad dream... :) ) Thanks strogm
 
<So, here's a short VB2008 program

Oh no...he's finally cracked...[cry]
 
Bob, just because I can write a VB2008 program doesn't mean I like to ... note in particular that it is really just a VB6 program indisguise.

Now, if I'd taken the route of pointing out that we can natively go multithreaded with VB2008, and could put the loop into a BackgroundWorker class, or a little more daringly directly spawned and managed a seperate thread ourselves ...
 
Thanks for your interest in my VB education. I'm learning.
BTW, it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top