Custom Progressbar with different color

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
Can a progressbar have different colors for different %? Like i want to set 6-9% to Red, 15-23 to Blue, Etc.
 
The standard ProgressBar control is drawn by Windows so you don't have control over the appearance. If you want a custom appearance then you'd have to create your own control and either use Images or draw the bar yourself using GDI+. In that case it can look however you want.
 
One custom progress bar control

I found this custom control somewhere on the internet a while ago.

I use them by creating a new class called "CustomControls.vb" (Ctrl + Shift + A) will allow you to add a net class.


Past this is side the file somewhere (it does not need to be within the default class)




HTML:
Public Class MetroProgressbar
    Inherits Control
 
    Dim _BorderColor As Color = Color.Black
    Property BorderColor As Color
        Get
            Return _BorderColor
        End Get
        Set(ByVal value As Color)
            _BorderColor = value
            Invalidate()
        End Set
    End Property
 
    Dim _ProgressColor As Color = Color.FromArgb(10, 150, 40)
    Property ProgressColor As Color
        Get
            Return _ProgressColor
        End Get
        Set(ByVal value As Color)
            _ProgressColor = value
            Invalidate()
        End Set
    End Property
 
    Sub New()
        SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.AllPaintingInWmPaint, True)
        DoubleBuffered = True
    End Sub
 
    Dim _Val As Integer = 0
    Property Value As Integer
        Get
            Return _Val
        End Get
        Set(ByVal v As Integer)
            If v <= _Max Then _Val = v Else Throw New Exception("The entered value is not valid.")
            Invalidate()
        End Set
    End Property
 
    Dim _Max As Integer = 100
    Property Maximum As Integer
        Get
            Return _Max
        End Get
        Set(ByVal value As Integer)
            If value >= _Val Then _Max = value Else Throw New Exception("The entered value is not valid.")
        End Set
    End Property
 
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim G As Graphics = e.Graphics
        Dim Progress As Double = (_Val / _Max) * (Width - 2)
        G.Clear(BackColor)
        G.FillRectangle(New SolidBrush(Color.FromArgb(20, Color.Black)), New Rectangle(0, 0, Width - 1, Height - 1))
        If Progress > 0 Then G.FillRectangle(New SolidBrush(_ProgressColor), New Rectangle(1, 1, CInt(Progress), Height - 2))
 
        G.DrawRectangle(New Pen(_BorderColor), New Rectangle(0, 0, Width - 1, Height - 1))
        MyBase.OnPaint(e)
    End Sub
End Class ' MetroProgressbar


Just click Build>Rebuild And you will see the control in your Toolbox.


Just give that a try and see what you come up with.

Google "cloudtoolkitn6 vb net" for a lot of cool controls.
 
Back
Top