how to customize a progress bar using vb.net

那年仲夏 提交于 2021-02-18 19:48:39

问题


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


回答1:


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



回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/20558871/how-to-customize-a-progress-bar-using-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!