I'm still using VB6 unfortunetly, but anyway, how would I go about implementing this?
Say I wanted to do something so when you click a button the text in a textbox changes but has a 200-300 millisecond delay so it looks as if the words are changing really fast. But whenever I try to do it, I click the button to test it, and it waits like 4 seconds(freezes), then displays the last word I put for it to display. How do I fix that?

Here's an example:

Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub cmdProduceWords_Click()
txtWords.Text = "Hello"
Sleep 300
txtWords.Text = "Cookie"
Sleep 300
txtWords.Text = "Train"
Sleep 300
txtWords.Text = "Cat"

And if I were to click it, it would pause for a while, maybe freeze, then display "Cat".

Recommended Answers

All 6 Replies

Why not put the code into a timer?

Your freezing really isnt the issue, you code seems fine. The problem is that the app doesnt have enough time to refresh the text boxes.

So the quick and simple solution would be >>>

txtWords.Text = "Hello"
Sleep 300
txtWords.Text = "Cookie"
txtWords.refresh
Sleep 300
txtWords.Text = "Train"
txtWords.refresh
Sleep 300
txtWords.Text = "Cat"

Job Done ? :cheesy:


I'm still using VB6 unfortunetly, but anyway, how would I go about implementing this?
Say I wanted to do something so when you click a button the text in a textbox changes but has a 200-300 millisecond delay so it looks as if the words are changing really fast. But whenever I try to do it, I click the button to test it, and it waits like 4 seconds(freezes), then displays the last word I put for it to display. How do I fix that?

Here's an example:

-------
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
-------
Private Sub cmdProduceWords_Click()
txtWords.Text = "Hello"
Sleep 300
txtWords.Text = "Cookie"
Sleep 300
txtWords.Text = "Train"
Sleep 300
txtWords.Text = "Cat"
-------


And if I were to click it, it would pause for a while, maybe freeze, then display "Cat".

Right, Paul had the right answer. You'll also find (which may be a better solution, since the .refresh method doesn't give windows a chance to update anything other than the textbox) that the doevents function will also work. Doevents, unlike .refresh, gives your app a chance to process events passed by the user. An example of this, is to stick another button on that form, for "quit", and have it have some unload and end code (like so):

for each XFrm in Forms
     unload XFrm
next XFrm

' /* Code Should Never Reach Here, But Just In Case */
End

Then, click your button to make the words change, and right after, click the Quit button. With the .refresh method, you'll see that it takes a lag time before ending the application. If you use the Doevents function, your application will respond more promptly to device control. The example I have here isn't as extreme as it could be (say, by having another textbox in which you type in at the same time), but it's enough to show you the difference between .refresh and doevents.

TxtWords.Text = "Hello"
DoEvents
Sleep 300
TxtWords.Text = "Cookie"
DoEvents
Sleep 300
TxtWords.Text = "Train"
DoEvents
Sleep 300
TxtWords.Text = "Cat"

I agree with Comatose - this is a much better way.

Actually I would disagree with this, refresh is the appropriate command to use here. DoEvents is for allowing further user interaction whilst code is executing, not for refreshing controls on a form.

See here for a fuller explanation
http://www.devx.com/vb2themax/Tip/18646


Regards

D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.