INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How do I exit a loop with a button_click event

How do I exit a loop with a button_click event

(OP)
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.
 

RE: How do I exit a loop with a button_click event

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

 

RE: How do I exit a loop with a button_click event

(OP)
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.

RE: How do I exit a loop with a button_click event

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

 

RE: How do I exit a loop with a button_click event

(OP)
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.  

RE: How do I exit a loop with a button_click event

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

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

RE: How do I exit a loop with a button_click event

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

RE: How do I exit a loop with a button_click event

<So, here's a short VB2008 program

Oh no...he's finally cracked...cry

RE: How do I exit a loop with a button_click event

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

RE: How do I exit a loop with a button_click event

(OP)
Thanks for your interest in my VB education. I'm learning.
BTW, it works.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close