2,892 questions
- Add a public
Text
property to hold the text value. - Invalidate the control when the text changes to force a redraw.
- Use that property in the
AdditionalPaint
method instead of the static string"Blah blah"
.
Here's the modified class with the dynamic text capability:
Public NotInheritable Class ProgressBarZ
Inherits ProgressBar
Private _displayText As String = String.Empty
Public Sub New()
MyBase.New()
Style = ProgressBarStyle.Continuous
SetStyle(ControlStyles.UserPaint, True) ' Optional: to take full control over painting
End Sub
Protected Overrides ReadOnly Property CreateParams As CreateParams
Get
Dim MyCP As CreateParams = MyBase.CreateParams
MyCP.ExStyle = MyCP.ExStyle Or &H200000 ' WS_EX_TRANSPARENT
Return MyCP
End Get
End Property
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
If m.Msg = &HF Then AdditionalPaint(m) ' WM_PAINT
End Sub
''' <summary>
''' Property to get/set the display text.
''' </summary>
Public Property DisplayText As String
Get
Return _displayText
End Get
Set(value As String)
If _displayText <> value Then
_displayText = value
Invalidate() ' Force redraw to update the text
End If
End Set
End Property
''' <summary>
''' Paints the text on top of the progress bar.
''' </summary>
Public Sub AdditionalPaint(ByVal m As Message)
If String.IsNullOrEmpty(_displayText) Then Return
Using g As Graphics = Graphics.FromHwnd(Handle)
Dim rect As New Rectangle(0, 0, Width, Height)
Dim format As New StringFormat(StringFormatFlags.NoWrap) With {
.Alignment = StringAlignment.Center,
.LineAlignment = StringAlignment.Center
}
Using textBrush As New SolidBrush(ForeColor)
g.DrawString(_displayText, Font, textBrush, rect, format)
End Using
End Using
End Sub
End Class
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin