Redraw ProgressBar in StatusBar

  • Thread starter Thread starter Nicolas
  • Start date Start date
N

Nicolas

When I resize (Enlarge) the form while the progress bar is running the
rectangle drawing is not going to the end of the rectangle?

I got a form with one button, one statusbar with three statusbarpanel

Here is the code for help

What is wrong there?

#Region "ProgressBar"

Public Structure progresspanel

Dim x As Integer

Dim y As Integer

Dim Width As Integer

Dim Height As Integer

End Structure

Dim pb As Drawing.Drawing2D.LinearGradientBrush

Dim myProgressPanel As progresspanel

Dim g As Graphics

Dim rect As Rectangle

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim t As New Threading.Thread(AddressOf progress)

t.Start()

End Sub

Private Sub StatusBar1_DrawItem(ByVal sender As Object, ByVal sbdevent As
System.Windows.Forms.StatusBarDrawItemEventArgs) Handles StatusBar1.DrawItem

myProgressPanel.x = sbdevent.Bounds.X

myProgressPanel.y = sbdevent.Bounds.Y

myProgressPanel.Width = sbdevent.Bounds.Width

myProgressPanel.Height = sbdevent.Bounds.Height

End Sub

Private Sub StatusBar1_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles StatusBar1.Resize

StatusBar1.Invalidate(rect)

End Sub

Sub progress()

Dim i, j, w As Integer

j = 10000

g = Me.StatusBar1.CreateGraphics

For i = 0 To j

w = CInt((myProgressPanel.Width / j) * i)

If w = 0 Then w = 1

rect = New Rectangle(myProgressPanel.x, myProgressPanel.y, w,
myProgressPanel.Height)

pb = New Drawing.Drawing2D.LinearGradientBrush(rect, Color.AntiqueWhite,
Color.LightBlue, Drawing.Drawing2D.LinearGradientMode.Vertical)

g.FillRectangle(pb, rect)

g.DrawString(w.ToString, New Font("Arial", 10, FontStyle.Regular,
GraphicsUnit.Point), New SolidBrush(Color.Red), myProgressPanel.x,
myProgressPanel.y)

Me.StatusBar1.Panels(0).Text = w.ToString

Next

Me.StatusBar1.Panels(0).Text = "0"

g.Flush()

End Sub

#End Region
 
Nicolas said:
When I resize (Enlarge) the form while the progress bar is running
the rectangle drawing is not going to the end of the rectangle?

I got a form with one button, one statusbar with three
statusbarpanel

Here is the code for help

What is wrong there?
Code:
[/QUOTE]

I tried your code but I can not reproduce the problem. I can resize the form
(resizing the docked statusbar (resizing the panel)) during painting the
panel in the other thread and the panel is always filled completely.
 
To rreproduce the error that I got:

1) launch the application (should not be maximized) - (if launch full screen
no problem)
2) press the button to start the progress bar
3) while the progress bar is incrementing, grab the right side of the form
to strech widely the form then release the mouse before the progressbar
stop. The progress bar counter should still increment (indicating the width
of the statusbarpanel) but the drawing is not going anyfurther then his
original maximum width instead of going to the end of the panel.

Thank you for your help if you find anything




Armin Zingler said:
Nicolas said:
When I resize (Enlarge) the form while the progress bar is running
the rectangle drawing is not going to the end of the rectangle?

I got a form with one button, one statusbar with three
statusbarpanel

Here is the code for help

What is wrong there?
Code:
[/QUOTE]

I tried your code but I can not reproduce the problem. I can resize the form
(resizing the docked statusbar (resizing the panel)) during painting the
panel in the other thread and the panel is always filled completely.
[/QUOTE]
 
Hi,
I know this is not of help to you but I'd like to ask you help me make a
progress bar. I need to make a pop up progress bar (on a form) that will
show progress of a file download.

Can you help me with that.

Thanks,
 
Nicolas said:
To rreproduce the error that I got:

1) launch the application (should not be maximized) - (if launch full
screen no problem)
2) press the button to start the progress bar
3) while the progress bar is incrementing, grab the right side of the
form to strech widely the form then release the mouse before the
progressbar stop. The progress bar counter should still increment
(indicating the width of the statusbarpanel) but the drawing is not
going anyfurther then his original maximum width instead of going to
the end of the panel.

That's what I've already done, but it was not reproducable. Now I did it
again, and, you are right, I can reproduce it. Well, when the graphics
object is created the width is smaller. If you resize the panel in the
meantime, you probably have to destroy the graphics object and create a
new one.
 
Thank you, I think it work with this approach
Private Sub StatusBar1_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles StatusBar1.Resize

g = Nothing

StatusBar1.Invalidate(rect)

g = Me.StatusBar1.CreateGraphics

End Sub
 
Nicolas said:
Thank you, I think it work with this approach
Private Sub StatusBar1_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles StatusBar1.Resize

Before setting the referenct to Nothing:

g.Dispose
 
Sorry Armin,

I don't get it. I don't use status bar and I don't use a graphic object.

Can you explain to me little bit more how to use what you just posted.

I appreciate your help
 
Dino M. Buljubasic said:
Sorry Armin,

I don't get it. I don't use status bar and I don't use a graphic
object.

Can you explain to me little bit more how to use what you just
posted.

??

I only added "g.dispose" to Nicolas' code because the Dispose method of
disposable objects should be called before setting the reference to Nothing.
 
Thanks, work great now

Armin Zingler said:
??

I only added "g.dispose" to Nicolas' code because the Dispose method of
disposable objects should be called before setting the reference to Nothing.
 
Actually this is better with a little check before isNothing() otherwise it
doesn't "g" at this point to dispose it. the resize event is call event
before I got it setup..
Private Sub StatusBar1_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles StatusBar1.Resize

If Not IsNothing(g) Then

g.Dispose()

g = Nothing

StatusBar1.Invalidate(rect)

g = Me.StatusBar1.CreateGraphics

End If

End Sub
 
Back
Top