-
Jul 8th, 2011, 06:55 PM
#1
Thread Starter
Member
Resolved: VB2005, how to refresh labels/textboxes
Hi.
I remember that in VB5 there was an "auto-redraw" property that needed to be turned on in order to make certain features refresh themselves. I'm having trouble with getting labels and text boxes to change in VB2005.
A sample code snip would be
Code:
Label1.text = "Zero"
Sleep (1000)
Label1.text = "One"
Sleep (1000)
Label1.text = "Two"
Sleep (1000)
...
Label1.text = "Five"
EndSub
Which should make Label1.text count upwards at one-second intervals.
When I run the code, however, Label1.text shows "Zero" for five seconds, then says "Five" when the subroutine ends. How can I make the label refresh after each change?
If I use a textbox instead of a label, the same thing happens.
(this uses the API call "Sleep" to insert a pause, declaration not shown here)
Any thoughts?
Last edited by Og_ofthejungle; Jul 12th, 2011 at 12:23 AM.
Reason: Resolved.
-
Jul 8th, 2011, 07:09 PM
#2
Re: VB2005, how to refresh labels/textboxes
the sleep code stops all code processing in that thread + also stops windows messages being processed.
try this:
vb Code:
Label1.text = "Zero"
label1.invalidate
Sleep (1000)
Label1.text = "One"
label1.invalidate
Sleep (1000)
Label1.text = "Two"
label1.invalidate
Sleep (1000)
...
Label1.text = "Five"
label1.invalidate
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 8th, 2011, 07:11 PM
#3
Re: VB2005, how to refresh labels/textboxes
instead of the API, there's a .Net method:
vb Code:
threading.thread.sleep(millisecondsTimeout as integer)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 8th, 2011, 07:13 PM
#4
Thread Starter
Member
Re: VB2005, how to refresh labels/textboxes
Interesting. Thanks, I'll try that.
-
Jul 8th, 2011, 07:15 PM
#5
Thread Starter
Member
Re: VB2005, how to refresh labels/textboxes
Originally Posted by .paul.
instead of the API, there's a .Net method:
vb Code:
threading.thread.sleep(millisecondsTimeout as integer)
Thanks, I finally settled on the "Sleep" method after trying a few tricks for "do while loop" and "for i, next i" but had trouble getting the timing right.
I'll give this a try.
-
Jul 11th, 2011, 12:26 AM
#6
Thread Starter
Member
Re: VB2005, how to refresh labels/textboxes
Okay, I tried both of those tricks, the label1.invalidate and the thread.threading.sleep() (one at a time). I also played with the label1.show().
The code still executes the same way: A long pause, and then the last text shown. I'm starting to think that I'll have to have the user step through the code using multiple "next" buttons, which would be much less elegant.
Any other suggestions or thoughts on this?
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "One"
'Label1.Invalidate()
Threading.Thread.Sleep(1000)
'Label1.Show()
Label1.Text = "two"
'Label1.Invalidate()
Threading.Thread.Sleep(1000)
'Label1.Show()
Label1.Text = "three"
'Label1.Invalidate()
Threading.Thread.Sleep(1000)
'Label1.Show()
Label1.Text = "Four"
'Label1.Invalidate()
'Label1.Show()
'Threading.Thread.Sleep(1000)
'Label1.Show()
'Label1.Text = "Five"
'Label1.Show()
End Sub
-
Jul 11th, 2011, 01:04 AM
#7
Re: VB2005, how to refresh labels/textboxes
So why not use the appropriate way for what you need to do?
You need your label to show something at one second interval. So a timer is more appropriate in this case.
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Not Timer1.Enabled Then Timer1.Interval = 1000 Timer1.Enabled = True End If End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Static counter As Integer = 0 counter += 1 Label1.Text = Choose(counter, "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten") If counter >= 10 Then counter = 0: Timer1.Enabled = False '' or whatever way you want it to stop End Sub
Last edited by Pradeep1210; Jul 11th, 2011 at 01:10 AM.
-
Jul 11th, 2011, 01:08 AM
#8
Re: VB2005, how to refresh labels/textboxes
If what you posted was just an example to show that you mean "some long running process" then you should consider Threading. Don't run your process on the UI thread in such cases. Instead you should run it on a different thread so that the UI thread is free to do what it is meant for (i.e. maintaining proper UI). You can consider the BackgroundWorker control in such cases or create threads using System.Threading.Thread classes.
-
Jul 11th, 2011, 09:53 AM
#9
Re: VB2005, how to refresh labels/textboxes
Whilst I would encourage you to listen to the advice above, if you are just coding something quickly for yourself (or lazy like me ) you could always do it like:
Code:
Label1.Text = "One"
Label1.Refresh()
Threading.Thread.Sleep(1000)
'..etc....
'.........
'or even.....
Label1.Text = "One"
Application.DoEvents()
Threading.Thread.Sleep(1000)
'..etc....
'.........
-
Jul 11th, 2011, 10:16 AM
#10
Re: VB2005, how to refresh labels/textboxes
Hmmmmm
Code:
Public Class Form1
Dim stpw As New Stopwatch
Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
Timer1.Interval = 100
Timer1.Start()
Button2.PerformClick()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'start
Button1.Enabled = False
Button2.Enabled = True
stpw.Restart()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
'stop
Button1.Enabled = True
Button2.Enabled = False
stpw.Stop()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If stpw.IsRunning Then
Label1.Text = stpw.Elapsed.TotalSeconds.ToString("n0")
End If
End Sub
End Class
@pradeep - I think we both know that using the timer to keep track of time is not a good idea.
-
Jul 11th, 2011, 10:19 AM
#11
Re: VB2005, how to refresh labels/textboxes
Originally Posted by Inferrd
Whilst I would encourage you to listen to the advice above, if you are just coding something quickly for yourself (or lazy like me ) you could always do it like:
Code:
Label1.Text = "One"
Label1.Refresh()
Threading.Thread.Sleep(1000)
'..etc....
'.........
'or even.....
Label1.Text = "One"
Application.DoEvents()
Threading.Thread.Sleep(1000)
'..etc....
'.........
As long as this code is just for play, why not. If this code is intended for actual use in an application, then NO!
-
Jul 11th, 2011, 12:05 PM
#12
Re: VB2005, how to refresh labels/textboxes
Originally Posted by dbasnett
@pradeep - I think we both know that using the timer to keep track of time is not a good idea.
Yes, my code was was assuming that a very precision interval is not needed.
Threading is the ideal way to go anyways.
-
Jul 11th, 2011, 06:42 PM
#13
Thread Starter
Member
Re: VB2005, how to refresh labels/textboxes
Originally Posted by Inferrd
Whilst I would encourage you to listen to the advice above, if you are just coding something quickly for yourself (or lazy like me ) you could always do it like:
Code:
Label1.Text = "One"
Label1.Refresh()
Threading.Thread.Sleep(1000)
'..etc....
'.........
'or even.....
Label1.Text = "One"
Application.DoEvents()
Threading.Thread.Sleep(1000)
'..etc....
'.........
This looks like what I'm looking for.
I would love to use a more complex timer routine -- the timer in VB5 was much simpler, as I recall it -- but I tend to go for the simple answer on the quick and dirty programs.
I'll try this one first.
-
Jul 11th, 2011, 06:54 PM
#14
Thread Starter
Member
Re: VB2005, how to refresh labels/textboxes
@ dbasnett & pradeep:
Why is using a timer to keep track of time a bad idea, and why would threading.thread.sleep(1000) not be good for an application? Is it because the entire application sleeps, and not just the thread?
Also, am I correct to infer that the timer_tick event is the action which occurs when the timer has expired its interval?
Thanks for the info.
-
Jul 11th, 2011, 07:29 PM
#15
Re: VB2005, how to refresh labels/textboxes
if you have a timer with interval = 500 it ticks every 1/2 second
the sleep function stops the entire thread, so if you use it on your GUI (primary) thread it'll freeze your app.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 11th, 2011, 10:47 PM
#16
Re: VB2005, how to refresh labels/textboxes
Originally Posted by Og_ofthejungle
@ dbasnett & pradeep:
Why is using a timer to keep track of time a bad idea, and why would threading.thread.sleep(1000) not be good for an application? Is it because the entire application sleeps, and not just the thread?
Also, am I correct to infer that the timer_tick event is the action which occurs when the timer has expired its interval?
Thanks for the info.
Timer is not a bad idea for normal application use. It is a bad idea for situations where you need a very high precision timer. It is not guaranteed to fire the ticks at EXACTLY at designated interval. It may vary by a few milliseconds or nanoseconds.
It is almost same as your VB5/6 timer. So you can continue to use the timer the same way you used in VB5/6.
Calling Threading.Thread.Sleep on the UI thread is a bad idea because it will hold the UI thread for that specified interval of time. Besides executing you code, the UI thread also has the responsibility of maintaining the GUI. So if you block that thread, it will not refresh properly during that time and your application will seem to hang. So even if you need to call it on the UI thread, call it for very short intervals.
e.g. Threading.Thread.Sleep(1000) will block the thread for 1 sec. So your screen will not refresh for 1 second if you call on UI thread. While calling multiple times with say Threading.Thread.Sleep(100) and Application.DoEvents might give it some time to refresh the GUI in between. The best however is to design your application in such a way that you don't need to call it on the UI thread at all. So you do your work on secondary threads and let the UI thread do what it is really meant for.
-
Jul 12th, 2011, 12:15 AM
#17
Thread Starter
Member
Re: VB2005, how to refresh labels/textboxes
Originally Posted by Inferrd
Whilst I would encourage you to listen to the advice above, if you are just coding something quickly for yourself (or lazy like me ) you could always do it like:
Code:
Label1.Text = "One"
Label1.Refresh()
Threading.Thread.Sleep(1000)
'..etc....
'.........
'or even.....
Label1.Text = "One"
Application.DoEvents()
Threading.Thread.Sleep(1000)
'..etc....
'.........
This worked beautifully. Thank you.
-
Jul 12th, 2011, 12:21 AM
#18
Thread Starter
Member
Re: VB2005, how to refresh labels/textboxes
Thank you all for the information. I appreciate the effort and the willingness to explain.
-
Jul 12th, 2011, 12:31 AM
#19
Re: Resolved: VB2005, how to refresh labels/textboxes
With regards to repainting a control, the methods of interest are Invalidate, Update and Refresh. The Invalidate method tells the system that a particular area of the control needs to be repainted, but it doesn't actually repaint it. That's why .paul.'s original code failed. You can call Invalidate and pass a Region or Rectangle to describe the area that needs repainting or neither to specify that the whole control needs repainting. You might call Invalidate several times and specify several different areas before the control actually gets repainted.
The Update method tells the system to repaint all the areas that have been invalidated since the last paint. The Refresh method is equivalent to calling Invalidate with no arguments followed by Update, which is exactly what it does internally.
With that in mind, it would be most correct to call Update rather than Refresh in this case. Setting the Text property will already have invalidated the appropriate area of the control, so calling Invalidate again is pointless and calling Invalidate with no arguments is wasteful.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|