Thomas Dutoit Thomas Dutoit - 3 months ago 26
Vb.net 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
duuh

Answer

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
        Get
            Return mVal
        End Get
        Set(value As Int32)

            If mVal <= MaxValue AndAlso mVal >= MinValue Then
                mVal = value
                mValue = mVal / MaxValue
                Me.Invalidate()
            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
        Get
            Return meter
        End Get
        Set(value As Boolean)
            If meter <> value Then
                meter = value
                Me.Invalidate()
            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
            MyBase.OnPaint(e)
            Return
        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.OnHandleCreated(e)
        MyBase.AutoSize = False
    End Sub

End Class

Notes

  • 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