Show Text on ProgressBar

StewartBW 1,765 Reputation points
2025-05-25T15:32:33.2966667+00:00

Hi,

To show different text strings on my ProgressBar at runtime, I have this code which prints a static text on ProgressBar, I need to add a Text property to the ProgressBar, any idea how to implement properly?

Here's the full ProgressBar class:

Public NotInheritable Class ProgressBarZ
    Inherits ProgressBar
    Public Sub New()
        MyBase.New()
        Style = ProgressBarStyle.Continuous
    End Sub
    Protected Overrides ReadOnly Property CreateParams As CreateParams
        Get
            Dim MyCP As CreateParams = MyBase.CreateParams
            MyCP.ExStyle = MyCP.ExStyle Or &H200000 ' or &H2000000 ??? For WndProc Text on Progress
            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
    Public Sub AdditionalPaint(ByVal m As Message)
        'If DisplayType = TextDisplayType.None Then
        '    Return
        'End If
        Dim text As String = "Blah blah"
        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(Color.Black)
                g.DrawString(text, Font, textBrush, rect, format)
            End Using
        End Using
    End Sub
End Class

Thanks in advance :)

Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. Marcin Policht 49,640 Reputation points MVP Volunteer Moderator
    2025-05-25T15:41:58.57+00:00
    1. Add a public Text property to hold the text value.
    2. Invalidate the control when the text changes to force a redraw.
    3. 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

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.