Results 1 to 6 of 6

Thread: Small problem with label or textbox updates

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Location
    MS
    Posts
    22

    Small problem with label or textbox updates

    I have a small, yet annoying problem. When I perform a "FOR" or "DO", I want to update a label or textbox as the loop progresses, for example: if I want to display a percentage done while incrementing a ProgressBar. The problem is that the label or textbox will never update, it wont even clear when I restart the loop!

    simplified code example:

    The label clear and = i never happen, only the "Done!"

    Code:
    ProgressBar1.Value = 0
    ProgressBar1.Maximum = 150
    
    Label1.Text = " "
    
            For i = 0 To 150
    
                ProgressBar1.Increment(2)
                i += 1
                System.Threading.Thread.Sleep(5)
                
                Label1.Text = i
    
            Next
    
    Label1.Text = "Done!"

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    That is because you are sleeping the thread or otherwise processing which doesn't give the UI time to actually show the changes. Just add an Application.DoEvents right after you change the text in the loop and it should show then.

  3. #3
    Addicted Member Bazza81's Avatar
    Join Date
    Apr 2001
    Location
    Nottingham, UK
    Posts
    203
    VB Code:
    1. ProgressBar1.Value = 0
    2. ProgressBar1.Maximum = 150
    3.  
    4. Label1.Text = " "
    5.  
    6.         For i = 0 To 150
    7.  
    8.             ProgressBar1.Increment(2)
    9.             i += 1
    10.             System.Threading.Thread.Sleep(5)
    11.            
    12.             Label1.Text = i
    13.  
    14.             [B]Application.DoEvents()[/B]            
    15.  
    16.         Next
    17.  
    18. Label1.Text = "Done!"
    Who needs rhetorical questions anyway?


    Bazza NET - The place you want to be!

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Location
    MS
    Posts
    22
    Originally posted by Edneeis
    That is because you are sleeping the thread or otherwise processing which doesn't give the UI time to actually show the changes. Just add an Application.DoEvents right after you change the text in the loop and it should show then.

    Thanks!

  5. #5
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    does that take care of refreshing the control as well? I did one of these but all I used were longer loop times and a refresh statement. I heard that the application.doevents() isn't really desirable in any situation. dont know why.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    What Application.DoEvents does is allow/force all of the threads in a process (possibly other processes as well) to work or update. This is bad in that if you are looping and there is no need for it then it makes the looping thread slower since it spends time waiting for other threads. This is good because sometimes you wait other threads to update, like the UIThread, even though it will eat clock cycles.

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