A long standing complaint with the standard progress bar control is that there is no easy way to display text. In spite of numerous requests, Microsoft has not included this capability. The following code includes a class, CustomProgress which inherits the standard ProgressBar and adds such a feature. It is fairly easy to use. If you want to show a percentage progress you set Text to a null string and the percentage will be calculated from the Minimum and Maximum value properties. If you want a custom string then assign it to Text. Either the percentge or custom text will be displayed when you call the Update method.

You can include the class code in your project, or if you are more adventurous, you can create a custom control and add it to your toolbox.

3,912 Views
''                                                                                      
''  Name:                                                                               
''                                                                                      
''      TextProgressBar                                                                 
''                                                                                      
''  Description:                                                                        
''                                                                                      
''      Text code for a custom progress bar control. This extension to the standard     
''      progress bar allows for a text overlay that can display either a percentage     
''      complete value or any other custom string.                                      
''                                                                                      
''  Audit:                                                                              
''                                                                                      
''      2019-04-18  rj  Original code                                                   
''                                                                                      

Public Class Form1

    Private prgCopy As New CustomProgress

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.Controls.Add(prgCopy)
        prgCopy.Dock = DockStyle.Bottom
        prgCopy.Value = 0.0
        prgCopy.Minimum = 0.0
        prgCopy.Maximum = 100.0
        prgCopy.Style = ProgressBarStyle.Continuous

    End Sub

    Private Sub btnShowText_Click(sender As Object, e As EventArgs) Handles btnShowText.Click

        prgCopy.Text = "display any custom text"

        For i As Integer = 1 To 100
            System.Threading.Thread.Sleep(25)
            prgCopy.Value = i
            prgCopy.Update()
            My.Application.DoEvents()
        Next

    End Sub

    Private Sub btnShowPercent_Click(sender As Object, e As EventArgs) Handles btnShowPercent.Click

        prgCopy.Text = ""

        For i As Integer = 1 To 100
            System.Threading.Thread.Sleep(25)
            prgCopy.Value = i
            prgCopy.Update()
            My.Application.DoEvents()
        Next

    End Sub

End Class

''                                                                                      
''  Name:                                                                               
''                                                                                      
''      CustomProgress                                                                  
''                                                                                      
''  Description:                                                                        
''                                                                                      
''      ProgressBar that allows display of custom text or percentage progress.          
''                                                                                      
''  Custom Properties:                                                                  
''                                                                                      
''      Text:     String    If Null then % progress is calculated and displayed, else   
''                the value of the string is displayed.                                 
''                                                                                      
''  Audit:                                                                              
''                                                                                      
''      2019-04-18  rj  Original Code                                                   
''                                                                                      

Public Class CustomProgress

    Inherits ProgressBar

    Public Overrides Property Text As String

    Private Const WmPaint = 15

    'WndProc receives all messages directed to the current window. In order to
    'avoid overwriting your text, make sure the default code executes first,
    'then call the custom routine to display the overlid text.

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        If m.Msg = WmPaint Then PaintText()
    End Sub

    Private Sub PaintText()

        Dim s As String = Text

        'Display either a percentage or custom text

        If s = "" Then
            Dim percent = CInt((Value - Minimum) * 100 / (Maximum - Minimum))
            s = percent.ToString & "%"
        End If

        'Get the graphics object and calculate drawing parameters based on the current Font specs

        Using g = Me.CreateGraphics()
            Dim textSize = g.MeasureString(s, Me.Font)
            Using b = New SolidBrush(ForeColor)
                g.DrawString(s, Me.Font, Brushes.Black, Me.Width / 2 - textSize.Width / 2, Me.Height / 2 - textSize.Height / 2)
            End Using
        End Using

    End Sub

End Class
About the Author

I completed my Computer Science degree at the University of Manitoba in 1976. I did two and a half years of programming in medical research followed by twenty-nine years at Manitoba Hydro (electric utility). Most of that was spent on doing development and maintenance on an AGC/SCADA (real-time programming/process control) system. The last ten years of that was spent doing application and infrastructure support and development. I have programmed in FORTRAN (mostly), APL, PL/1, COBOL, Lisp, SNOBOL, ALGOL, Assembler (several flavours), C, C++, Paradox, VB, vbScript and more recently, Python. I am married with two grown children of whom I am very proud, and a most beautiful wife. I am currently retired (and loving it).

Nice code. I'd like to point out a couple of things though:

The code converting the progressbar value to a percentage string can be simplified by using integer division and interpolated string syntax:

            Dim percent = ((Value - Minimum) * 100) \ (Maximum - Minimum)
            s = $"{percent}%"

In the nested Using block you don't seem to be using the brush you create anywhere inside the block:

            Using b = New SolidBrush(ForeColor)
                g.DrawString(s, Me.Font, Brushes.Black, Me.Width / 2 - textSize.Width / 2, Me.Height / 2 - textSize.Height / 2)
            End Using

The second one (unused b) was leftover code from previous attempts while figuring the current version out. I just neglected to remove it. Thanks for noting what could be improved.

Scored 9 by me Sir..
Nice looking PeeBar..haha..