I've used the OnPaint even to write graphics to the screen before and it has always worked. This is the only the second time I've done text however and it doesn't want to work this time. In the past I was only drawing to the form though and not a control. I've tried a few different ways, but here is currently how the code looks.
Is it something about the OnPaint event itself that is causing it? I have found another way to do this, but I would prefer the control to write this to itself rather than always needing to remember to write it else where. Also, I wanted the OnPaint event so if something else caused it to repaint before the value updated it would still show the progress values and not wait until the value was updated.
-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
Code:
Public Class ProgressBarWithText
Inherits System.Windows.Forms.ProgressBar
Private MyBrush As New System.Drawing.SolidBrush(Drawing.Color.Black)
#Region "Properties"
Public Shadows Property Value() As Integer
Get
Return MyBase.Value
End Get
Set(ByVal value As Integer)
MyBase.Value = value
RaiseEvent ValueChanged(Me)
End Set
End Property
#End Region
#Region "Events"
Public Event ValueChanged(ByVal sender As Object)
#End Region
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
If Me.Visible = True Then
Dim x As Single
Dim y As Single
Dim text As String = Me.Value & "/" & Me.Maximum
Dim sizeF As System.Drawing.SizeF = Me.CreateGraphics.MeasureString(text, Me.Font, Me.Width)
x = (Me.Width / 2) - (sizeF.Width / 2)
y = (Me.Height / 2) - (sizeF.Height / 2)
e.Graphics.DrawString(text, Me.Font, Me.MyBrush, x, y)
End If
MyBase.OnPaint(e)
End Sub
End Class
-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!