JasonBourne
JasonBourne

Reputation: 33

how to customize a progress bar using vb.net

I was trying to creating a progress bar that was color coded in relation to the value. For example, from 0-35, the progress bar should be red-colored and above 35, green colored. Any idea how I can go about doing it?

If ProgressBar1.Value >= 35 Then
ProgressBar1.BackColor = Color.Green
Else
ProgressBar1.BackColor = Color.Red
End If

P.S in the same progressbar, both the colors have to shown based on the values

Upvotes: 1

Views: 23168

Answers (3)

Sunusi Mohd Inuwa
Sunusi Mohd Inuwa

Reputation: 74

You can possibly use this method.

Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Enum ProgressBarColor
    Green = &H1
    Red = &H2
    Yellow = &H3
End Enum
Private Shared Sub ChangeProgBarColor(ByVal ProgressBar_name As Windows.Forms.ProgressBar, ByVal ProgressBar_Color As ProgressBarColor)
    SendMessage(ProgressBar_name.Handle, &H410, ProgressBar_Color, 0)
End Sub

then add your condition statement by calling the function above e.g.

 If ProgressBar1.Value >= 35 Then
     ChangeProgBarColor(ProgressBar1, ProgressBarColor.Red)
 Else
     ChangeProgBarColor(ProgressBar1, ProgressBarColor.Yellow)
 End If

Upvotes: 0

Edper
Edper

Reputation: 9322

You need to change settings on this one.

Go to Project --> [WindowsApplication] Properties

On Application Tab -- Uncheck Enable Visual Styles

However, be warned for there is a visual change on your progress bar as you will see.

You could then probably code like this:

If (ProgressBar1.Value > 35) Then
    ProgressBar1.ForeColor = Color.Red
Else
    ProgressBar1.ForeColor = Color.Green
End If

Upvotes: 2

Levi
Levi

Reputation: 1983

The position of the progress bar is stored in ProgressBar1.value. You can check this value in an If statement and change the color using ProgressBar1.ForeColor

eg:

If ProgressBar1.value > 35 Then
    ProgressBar1.ForeColor = Color.Lime
End If

I hope this helps ;)

Edit: Try using ProgressBar1.ForeColor rather than ProgressBar1.BackColor

Upvotes: 0

Related Questions