12

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.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42

4 Answers4

26

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

techno
  • 6,100
  • 16
  • 86
  • 192
  • 1
    Note 1: This will also remove visual styles from all other controls (e.g. TextBox, Button). Note 2: Question is about WinForms. – hfrmobile Mar 12 '17 at 15:00
  • If this is WinForms, it _is_ actually possible. See [this answer](https://stackoverflow.com/a/34268607/395685). – Nyerguds Mar 29 '18 at 14:12
5

Find and remove Application.EnableVisualStyles(); from your aplication.

you can find many examples from here

Damith
  • 62,401
  • 13
  • 102
  • 153
2

Red tends to indicate errors or troubles -- please reconsider using red to indicate "strong password".

Also, because you're updating the color many many times based on potentially many matches, your colors won't be as consistent as you'd like.

Instead, give each of the conditions a score, and then choose your color based on the total score:

    int score = 0;

    if (txtPass.Text.Length < 4)
        score += 1;
    if (txtPass.Text.Length >= 6)
        score += 4;
    if (txtPass.Text.Length >= 12)
        score += 5;
    if (Regex.IsMatch(PassChar, @"[a-z]") && Regex.IsMatch(PassChar, @"[A-Z]"))
        score += 2;
    if (Regex.IsMatch(PassChar, @"[!@#\$%\^&\*\?_~\-\(\);\.\+:]+"))
        score += 3;

    if (score < 2) {
       color = Color.Red;
    } else if (score < 6) {
       color = Color.Yellow;
    } else if (score < 12) {
       color = Color.YellowGreen;
    } else {
       color = Color.Green;
    }

Note the use of an else-if construct that is sometimes easier than language-supplied switch or case statement. (The C/C++ one in particular is prone to buggy software.)

sarnold
  • 102,305
  • 22
  • 181
  • 238
0

I KNOW this post is old, but my google foo brought me here in search of an answer to this question so someone else might end up here as well.

You can tell windows NOT to apply visual styles to your application allowing you to change the color of the progress bar by entering the following line at the start of your code

[System.Windows.Forms.Application]::VisualStyleState = 0

For a full list of Properties and Methods that can be used from [System.Windows.Forms.Application] refer to the documentation https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application?view=windowsdesktop-7.0

DBunting
  • 38
  • 6