-
I figured out all about DoEvents yesterday.
I'll try to explain:
Code:
Private Sub DoSomething()
Text1 = "working"
Do Until Text1 = "Quit"
DoEvents
Loop
Text1 = "Ready"
End Sub
Private Sub Command1_Click()
DoSomething
End Sub
Private Sub Command2_Click()
Text1 = "Quit"
Do Until Text1 = "Ready"
DoEvents
Loop
MsgBox "Hello"
End Sub
It never gets out of the Do..Loop in Command2.
Any suggestions to fix this or even a different thought process would be greatly appreciated.
-
Looks like you haven't quite got what the use of doevents is - look at this thread : http://forums.vb-world.net/showthrea...threadid=55745
-
I think your code is in a kind of catch 22 situation.
Try this code :
Code:
Private Sub DoSomething()
Text1 = "working"
Do Until Text1 = "Quit"
DoEvents
Loop
Text1 = "Ready"
MsgBox "Hello"
End Sub
Private Sub Command1_Click()
DoSomething
End Sub
Private Sub Command2_Click()
Text1 = "Quit"
End Sub
- jamie
-
What are you trying to do?
-
<?>
Code:
'Use a boolean variable to stop a For Next Loop
Option Explicit
Dim bGetOut As Boolean 'trigger to stop loop
Private Sub Command1_Click()
bGetOut = False 'reset if you want to start again
Dim myInt As Integer 'incremen
'display a random number as form caption in a loop
Do While Not bGetOut
Randomize 'Rnd Function
myInt = myInt + CInt(Rnd * 10) + 1 'create new caption
Me.Caption = myInt
DoEvents
Loop
End Sub
Private Sub Form_DblClick()
'dbl click the form to stop the loop
bGetOut = True
End Sub
-
Both of the examples that were given is basically the same as my original code w/o the Do..Loop in Command2.
The only problem with that is I haven't verified that I'm out of the other Loop before I move on to something else.
-
Well the example you gave is a little silly ...
Give us something more substantial and we'll see.
- jamie
-
I agree, it was silly but the structure was understood.
What it's actually doing is the user keys in a stock symbol and then the program begins to download a bunch of information for that stock which takes a while. In case the user changes their mind and keys in another stock I want to cancel grabbing all the previous stock info and start downloading the new stock info.
-
I think someone posted this kinda thing already in here.
However,
try the following ...
Code:
Private Sub DownloadData_Click()
Winsock1.Connect ...
End Sub
Private Sub Winsock1_Connect()
Do While DoLoop
'Download commands ...
Loop
End Sub
Private Sub StopDownload_Click()
DoLoop = False
End Sub
- jamie
-
<?>
the example was just to give you somthing to play with
Code:
Dim bReady As Boolean
Private Sub DoSomething()
Do While bReady = False
Me.Caption = CInt(Rnd * 120)
DoEvents
Loop
End Sub
Private Sub Command1_Click()
DoSomething
End Sub
Private Sub Command2_Click()
Do While bReady = True
DoEvents
Loop
MsgBox "Hello"
End Sub
-
HeSaidJoe, you reference bReady a few times in your example but you never set it or change it. How do any of the loops exit?
-
<?>
Code:
Dim bReady As Boolean
Private Sub DoSomething()
Do While Not bReady
Me.Caption = CInt(Rnd * 1200)
'reset in here when you get whatever you are looking for
If Me.Caption = 1000 Then
bReady = True
Command2_Click
Exit Do
End If
DoEvents
Loop
End Sub
Private Sub Command1_Click()
bReady = False
DoSomething
End Sub
Private Sub Command2_Click()
bReady = True
MsgBox "Hello"
End Sub