|
|
 |
| 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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

May 19th, 2008, 12:29 PM
|
|
Registered User
|
|
Join Date: May 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

May 19th, 2008, 01:06 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 1 Time in 1 Post
|
|
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...)
|

May 19th, 2008, 02:56 PM
|
|
Registered User
|
|
Join Date: May 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

May 23rd, 2008, 04:22 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 1 Time in 1 Post
|
|
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
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |