|
-
Feb 10th, 2008, 04:27 PM
#1
Thread Starter
Frenzied Member
[2005] ToolStripProgressBar Not Updating
I am performing a read/write operation within a function that usually takes around 2 seconds to complete. During this, I need to update certain labels and such using current values within the function. Normally, the UI wouldn't update until the function was complete, but this was remedied with Application.DoEvents in the loop, which allowed the labels and such to update during the long loop.
However, my client wants me to have a progress bar showing the current progress and how much time is remaining. I have no problem getting the % complete and changing the value of the progress bar each iteration, but for some reason, the progress bar is not updating until the end of the entire operation, which means it goes from 0 to 100 in one swoop.
I don't see why this sort of thing is happening because I am setting the value just like with all the labels, and I am still calling Application.DoEvents. I have tried using a BackgroundWorker, threads, Invoke, and everything of that nature, but cannot, for the life of me, get that darned progress bar to update during the loop.
Here is my entire function, which, I know looks messy, as I am just trying to get things to work now, and then going to fix it up later:
http://rafb.net/p/Tqo5uL10.html
-
Feb 10th, 2008, 07:47 PM
#2
Re: [2005] ToolStripProgressBar Not Updating
Try calling the Refresh method of the control to force it to redraw itself. Note that the ToolStripProgressBar is not actually a control. It's a component that hosts a control. You can access the ProgressBar control itself via the ProgressBar property, so you'd need to do something like:
vb.net Code:
myToolStripProgressBar.ProgressBar.Refresh()
-
Feb 10th, 2008, 10:43 PM
#3
Thread Starter
Frenzied Member
Re: [2005] ToolStripProgressBar Not Updating
Yea, I tried that already, but just tried it again to make sure, and it definitely isn't working. Any other suggestions?
-
Feb 10th, 2008, 11:20 PM
#4
Re: [2005] ToolStripProgressBar Not Updating
I just ran a test and found that the progress bar updated itself fine. Try this:
1. Create a new WinForms project.
2. Add a Button and a StatusStrip to the form.
3. Add a StatusLabel and a ProgressBar to the StatusStrip.
4. Add this code:
Code:
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles Button1.Click
Me.ToolStripStatusLabel1.Text = "0"
Me.ToolStripProgressBar1.Value = 0
For i As Integer = 1 To 100
Dim n As Integer = 0
For j As Integer = 1 To 10000000
n += 1
Next j
Me.ToolStripStatusLabel1.Text = i.ToString()
Me.ToolStripProgressBar1.Value = i
'Me.StatusStrip1.Refresh()
Next i
End Sub
5. Run the project and click the Button.
You'll see that the ProgressBar shows the progress as expected. The problem is that the StatusStrip doesn't repaint properly. Now uncomment the last line of the Button's Click event handler so you're calling the Refresh method of the StatusStrip each iteration. Run the project again and you'll see a smooth increase in the ProgressBar, without a need to call DoEvents.
If you can provide steps to reproduce the issue you're seeing then by all means do.
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
|