C#进度条改变颜色

10 c# colors progress-bar

我正在尝试更改进度条的颜色,我正在使用它作为密码强度验证器.例如,如果所需的密码较弱,则进度条将变为黄色,如果为中,则为绿色.坚强,橙色.非常强烈,红色.就是这样的.这是密码强度验证器的代码:

using System.Text.RegularExpressions;
using System.Drawing;
using System.Drawing.Drawing2D;

var PassChar = txtPass.Text;

        if (txtPass.Text.Length < 4)
        pgbPass.ForeColor = Color.White;
        if (txtPass.Text.Length >= 6)
        pgbPass.ForeColor = Color.Yellow;
        if (txtPass.Text.Length >= 12)
        pgbPass.ForeColor = Color.YellowGreen;
        if (Regex.IsMatch(PassChar, @"\d+"))
        pgbPass.ForeColor = Color.Green;
        if (Regex.IsMatch(PassChar, @"[a-z]") && Regex.IsMatch(PassChar, @"[A-Z]"))
        pgbPass.ForeColor = Color.Orange;
        if (Regex.IsMatch(PassChar, @"[!@#\$%\^&\*\?_~\-\(\);\.\+:]+"))
        pgbPass.ForeColor = Color.Red;
Run Code Online (Sandbox Code Playgroud)

pgbPass.ForeColor = Color.ColorHere似乎不起作用.有帮助吗?谢谢.

tec*_*hno 23

除非视觉样式已禁用,否则无法在c#中更改进度条颜色.尽管IDE提供更改颜色,但您将看不到颜色更改,因为进度条将占用当前操作系统的视觉样式.您可以选择禁用整个应用程序的视觉样式.要执行此操作,请转到程序的起始类并从代码中删除此行

 Application.EnableVisualStyles();
Run Code Online (Sandbox Code Playgroud)

或使用一些自定义进度条控件,如 http://www.codeproject.com/KB/cpp/colorprogressbar.aspx


Dam*_*ith 5

查找并Application.EnableVisualStyles();从您的应用程序中删除 。

你可以从这里找到很多例子