Hi, how do I;
Wait 3 seconds after a user has stopped typing in a specific text box in my program, and then carry out some code.
Printable View
Hi, how do I;
Wait 3 seconds after a user has stopped typing in a specific text box in my program, and then carry out some code.
Heres 1 way:
Place Timer control on form. Timer1.Enabled = false
Timer1.interval = 1000
Then if your textbox is Text1, this should work:
VB Code:
Dim I_CHANGED As Integer Private Sub Text1_Change() I_CHANGED = 0 Timer1.Enabled = True End Sub Private Sub Timer1_Timer() I_CHANGED = I_CHANGED + 1 If I_CHANGED > 2 Then MsgBox "DONE" 'Or Call the Sub that contains the code you want executed. Timer1.Enabled = False End If End Sub
-Lou
Try the Sleep API
VB Code:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Sub Form_Load() Debug.Print "Start" Debug.Print "Wait 3 Seconds" Sleep 3000 Debug.Print "Go" End Sub
BTW, you don't need a timer control to run the above code.Code:
Dim dblClock As Double
dblClock = Timer
While Timer < dblClock + 3 ' Wait 3 seconds
DoEvents
Wend
Might I suggest a slight mod?
This will stop it running all day if it just happens to execue at midnight.Code:
Dim dblClock As Double
dblClock = Timer
While abs(Timer - dblClock) < 3 ' Wait 3 seconds
DoEvents
Wend
Purists would probably want to use Now() instead of Timer, but I think it's not worth the trouble for the odd time the code may execute at midnight.
Now I'm sure somebody will say 'Yes, but the Timer uses less cpu time'. I don't buy that. The above code may SEEM to use more cpu time while the loop is running, but it should give up any time slices needed by other apps if required - so I don't think it matters. Must try it sometime with a test app and see if it has any real effect.