Search code examples
c#colorsprogress-bar

C# progress bar change color


I am trying to change the color of my progress bar, I'm using it as a password strength validator. For example, if the desired password is weak, the progress bar will turn yellow, if medium, then green. Strong, orange. Very strong, red. It's just something like that. Here's my code for the password strength validator:

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;

The pgbPass.ForeColor = Color.ColorHere doesn't seem to be working. Any help? Thanks.


Solution

  • The Progress Bar Color cannot be changed in c# unless the the Visual Styles are Disabled.Although the IDE Offers to change the Color you will observe no color change as the progress bar will take up the visual style of the current operating system.You can opt to disable the visual style for your whole application.To do this go to the starting class of the program and remove this line from the code

     Application.EnableVisualStyles();
    

    or use some custom progress bar control like this http://www.codeproject.com/KB/cpp/colorprogressbar.aspx