Results 1 to 19 of 19

Thread: Resolved: VB2005, how to refresh labels/textboxes

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    34

    Resolved 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.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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:
    1. Label1.text = "Zero"
    2. label1.invalidate
    3. Sleep (1000)
    4. Label1.text = "One"
    5. label1.invalidate
    6. Sleep (1000)
    7. Label1.text = "Two"
    8. label1.invalidate
    9. Sleep (1000)
    10. ...
    11. Label1.text = "Five"
    12. label1.invalidate

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB2005, how to refresh labels/textboxes

    instead of the API, there's a .Net method:

    vb Code:
    1. threading.thread.sleep(millisecondsTimeout as integer)

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    34

    Re: VB2005, how to refresh labels/textboxes

    Interesting. Thanks, I'll try that.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    34

    Re: VB2005, how to refresh labels/textboxes

    Quote Originally Posted by .paul. View Post
    instead of the API, there's a .Net method:

    vb Code:
    1. 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.

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    34

    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

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         If Not Timer1.Enabled Then
    3.             Timer1.Interval = 1000
    4.             Timer1.Enabled = True
    5.         End If
    6.     End Sub
    7.  
    8.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    9.         Static counter As Integer = 0
    10.         counter += 1
    11.         Label1.Text = Choose(counter, "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten")
    12.         If counter >= 10 Then counter = 0: Timer1.Enabled = False '' or whatever way you want it to stop
    13.     End Sub
    Last edited by Pradeep1210; Jul 11th, 2011 at 01:10 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Resolved 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....
            '.........

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: VB2005, how to refresh labels/textboxes

    Quote Originally Posted by Inferrd View Post
    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!
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: VB2005, how to refresh labels/textboxes

    Quote Originally Posted by dbasnett View Post
    @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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    34

    Re: VB2005, how to refresh labels/textboxes

    Quote Originally Posted by Inferrd View Post
    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.

  14. #14

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    34

    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.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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.

  16. #16
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: VB2005, how to refresh labels/textboxes

    Quote Originally Posted by Og_ofthejungle View Post
    @ 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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  17. #17

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    34

    Smile Re: VB2005, how to refresh labels/textboxes

    Quote Originally Posted by Inferrd View Post
    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.

  18. #18

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    34

    Re: VB2005, how to refresh labels/textboxes

    Thank you all for the information. I appreciate the effort and the willingness to explain.

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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
  •  



Click Here to Expand Forum to Full Width