Thomas Dutoit Thomas Dutoit - 1 year ago 97 Question

How to use a Label as a ProgressBar

I have a problem with a project I'm doing.

I have a panel that contains 8 labels (Label 1, Label 2, etc...)

What I want to do is change a label into a progress bar with text.
This happens dynamically.

Note: It has to change back.

Can I do this ?

Also, I have to set the progress bar colors to red and orange when a quota is reached.

I tried:

Label1 = Progressbar1
Progressbar1.value = 75

But that doesn't work

Answer Source

What I want to do is change a label into a progress bar with text. Doing so, it wont look like the standard progress bar with the animations. To compensate this will use a gradient:

Imports System.Drawing.Drawing2D

Public Class LabeledMeter
    Inherits Label

    Public Property MinValue As Int32 = 0
    Public Property MaxValue As Int32 = 100
    Private mVal As Int32 = 1
    Private mValue As Double
    Public Property Value As Int32
            Return mVal
        End Get
        Set(value As Int32)

            If mVal <= MaxValue AndAlso mVal >= MinValue Then
                mVal = value
                mValue = mVal / MaxValue
            End If
        End Set
    End Property

    Public Property StartColor As Color = Color.LimeGreen
    Public Property EndColor As Color = Color.Firebrick

    Private meter As Boolean = False
    Public Property MeterDisplay As Boolean
            Return meter
        End Get
        Set(value As Boolean)
            If meter <> value Then
                meter = value
            End If
        End Set
    End Property

    Public Sub New()
        MyBase.TextAlign = ContentAlignment.MiddleCenter
        MyBase.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        If MeterDisplay = False Then
        End If

        ' fun and games
        Dim c2 As Color = EndColor
        'If mValue < 0.51 Then
        '    c2 = Color.Yellow
        'End If

        Dim rect = New Rectangle(0, 0,
                        Convert.ToInt32(Width * mValue), Height)
        Using br As New LinearGradientBrush(rect, StartColor, c2, LinearGradientMode.Horizontal)
            e.Graphics.FillRectangle(br, rect)
        End Using

        ' could draw the Text - specs are not clear
        TextRenderer.DrawText(e.Graphics, mValue.ToString("P1"), MyBase.Font, ClientRectangle, MyBase.ForeColor)
    End Sub

    Protected Overrides Sub OnHandleCreated(e As EventArgs)
        MyBase.AutoSize = False
    End Sub

End Class


  • The MeterDisplay property toggles the metering or just standard label mode.
  • Value changes the percentage filled. It repaints only if MeterDisplay is True.
  • Text versus XX% test display could be property driven.
  • Lowering the alpha on the start color can sometimes provide good visuals

enter image description here

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download