delay text to speech until after label.text updates in vb.net
I am wondering if there is a simple way to make the text to speech occur after the updating of label.text
If I have the following:
label.Text = "words words"
voice.Speak(label.Text)
I would like the label on the form to display "wor开发者_开发问答ds words" before it speaks. I'm a beginner with vb, and the only thing I could come up with was to use a timer. Just wondering if there's a simpler/more sophisticated solution. Thanks for helping
Use the SpeakAsync() method so that the speech engine doesn't block your UI thread. That solves many problems, including the delayed painting of the label.
The trick is forcing the label to repaint itself with the new text before the Speak
method is called. Just because you assign a new text value to the control doesn't ensure that it gets immediately repainted with that new text. Generally, the system waits until it is idle to do redraws, but you're not letting it idle before you tell it to execute the Speak
method.
The easiest way to fix this in .NET is to call the Refresh
method. All controls have it, and it does exactly what you want here. It forces the control to invalidate its client area (meaning the part that you can see, including the text) and redraw it immediately.
Change your code to look like this:
label.Text = "words words"
label.Refresh()
voice.Speak(label.Text)
精彩评论