Wrox Programmer Forums
|
Visual Basic 2008 Essentials If you are new to Visual Basic programming with version 2008, this is the place to start your questions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2008 Essentials section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 19th, 2008, 11:29 AM
Registered User
 
Join Date: May 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Stop button won't stop loop

Here is a small program I have been playing with. At this point all it does is toggle the colors of the radio buttons.
I can't get the stop command button to stop the loop, can someone please help with what I am doing wrong. I have spent a couple weeks trying to figure it out myself.


Public Class Form1
    Dim T As Integer
    Dim A As Integer

    Public Declare Function GetTickCount Lib "kernel32" () As Long




    Private Sub Start1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start1.Click
        A = 1
        T = 300

        Do While A = 1
            RB1.BackColor = System.Drawing.Color.LightGreen
            Me.Refresh()
            wait(T)
            RB1.BackColor = System.Drawing.Color.Red
            Me.Refresh()

            RB2.BackColor = System.Drawing.Color.LightGreen
            Me.Refresh()
            wait(T)
            RB2.BackColor = System.Drawing.Color.Red
            Me.Refresh()

            RB3.BackColor = System.Drawing.Color.LightGreen
            Me.Refresh()
            wait(T)
            RB3.BackColor = System.Drawing.Color.Red
            Me.Refresh()

            RB4.BackColor = System.Drawing.Color.LightGreen
            Me.Refresh()
            wait(T)
            RB4.BackColor = System.Drawing.Color.Red
            Me.Refresh()

            RB5.BackColor = System.Drawing.Color.LightGreen
            Me.Refresh()
            wait(T)
            RB5.BackColor = System.Drawing.Color.Red
            Me.Refresh()


        Loop


    End Sub






    Public Sub wait(ByVal dblMilliseconds As Double)
        Dim dblStart As Double
        Dim dblEnd As Double
        Dim dblTickCount As Double

        dblTickCount = GetTickCount()
        dblStart = GetTickCount()
        dblEnd = GetTickCount + dblMilliseconds

        Do
            dblTickCount = GetTickCount()
        Loop Until dblTickCount > dblEnd Or dblTickCount < dblStart


    End Sub


    Private Sub Stop1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Stop1.Click
        A = 0

        RB1.BackColor = System.Drawing.Color.Blue
        RB2.BackColor = System.Drawing.Color.Blue
        RB3.BackColor = System.Drawing.Color.Blue
        RB4.BackColor = System.Drawing.Color.Blue
        RB5.BackColor = System.Drawing.Color.Blue


    End Sub


End Class

 
Old May 19th, 2008, 12:06 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Have you verified that Stop1_Click runs? If that button has been renamed so that " Handles Stop1.Click" refers to nothing, then the event won't run.

If Sub wait() ties up the computer, the click on the button might get ignored.
Or perhaps clicking the button does get noted by the computer, but when the Windows Message is dispatched, it gets ignored because the processor is busy.

Seems a first step. (You could change the caption of the form as proof...)
 
Old May 19th, 2008, 01:56 PM
Registered User
 
Join Date: May 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have verified that the button is named properly. When I click the stop1 first the radio buttons do change color to blue. I just tried taking out the delay function. The program still just loops and doesn't see the stop. Do I need to do something in the loop so it will check if the stop button has been clicked?
Thanks.

 
Old May 23rd, 2008, 03:22 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

This still misbehaves. You click the start, and it starts. You click the stop or the form's client area and the start button depresses.

Having done that, clicking the stop then functions. (?)
Code:
Public Class Form1
    Dim T As Integer
    Dim A As Integer

    Public Declare Function GetTickCount Lib "kernel32" () As Long

    Private Sub Start1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start1.Click
        A = 1
        T = 300

        Do While A = 1
            ProcessRB(1): If A = 0 Then Exit Do
            ProcessRB(2): If A = 0 Then Exit Do
            ProcessRB(3): If A = 0 Then Exit Do
            ProcessRB(4): If A = 0 Then Exit Do
            ProcessRB(5)
        Loop

    End Sub

    Public Sub ProcessRB(ControlNum As Integer)

        Dim c As Control
            c = Me.Controls("RB" & ControlNum)

        c.BackColor = System.Drawing.Color.LightGreen
        Me.Refresh()
        If A = 0 Then Exit Sub
        wait(T)
        If A = 0 Then Exit Sub
        c.BackColor = System.Drawing.Color.Red
        Me.Refresh()

    End Sub

    Public Sub wait(ByVal dblMilliseconds As Double)

        Dim dblStart As Double
        Dim dblEnd As Double
        Dim dblTickCount As Double

        dblTickCount = GetTickCount()
        dblStart = GetTickCount()
        dblEnd = GetTickCount + dblMilliseconds

        Do
            dblTickCount = GetTickCount()
            System.Windows.Forms.Application.DoEvents()
            If a = 0 Then Exit Do
        Loop Until dblTickCount > dblEnd Or dblTickCount < dblStart

    End Sub

    Private Sub Stop1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Stop1.Click

        A = 0

        RB1.BackColor = System.Drawing.Color.Blue
        RB2.BackColor = System.Drawing.Color.Blue
        RB3.BackColor = System.Drawing.Color.Blue
        RB4.BackColor = System.Drawing.Color.Blue
        RB5.BackColor = System.Drawing.Color.Blue

    End Sub

End Class





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to stop file uploading when a cancel button coolcoder2007 ASP.NET 2.0 Basics 0 October 26th, 2007 04:21 AM
How to know my COM+ stop start vanthq Pro VB Databases 0 January 6th, 2006 05:19 AM
Loop through records- stop if certain value met SoC Classic ASP Basics 2 July 28th, 2005 08:22 PM
Cannot stop the process mcinar SQL Server 2000 11 October 20th, 2004 07:39 AM
Mail Loop Will Randomly Stop Working jimmlegs VBScript 1 February 2nd, 2004 08:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.