C # progress bar color

I am trying to change the color of my progress bar, I use it as a means of checking the strength of the password. For example, if the desired password is weak, the progress bar will turn yellow, if average, then green. Strong, orange. Very strong, red. It is something like this. Here is my password validation code:

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, @"[az]") && Regex.IsMatch(PassChar, @"[AZ]")) pgbPass.ForeColor = Color.Orange; if (Regex.IsMatch(PassChar, @"[!@#\$%\^&\*\?_~\-\(\);\.\+:]+")) pgbPass.ForeColor = Color.Red; 

pgbPass.ForeColor = Color.ColorHere doesn't seem to work. Any help? Thanks.

+8
c # colors progress-bar
source share
3 answers

The color of the progress bar cannot be changed in C # unless visual styles are disabled. Although the IDE offers color changes, you will not notice a color change, because the progress bar will take on the visual style of the current operating system. You can turn off the visual style for your entire application. To do this, go to the start class of the program and remove this line from the code

  Application.EnableVisualStyles(); 

or use some custom runtime options like this http://www.codeproject.com/KB/cpp/colorprogressbar.aspx

+20
source share

Locate and remove Application.EnableVisualStyles(); from your application.

You can find many examples from here.

+4
source share

Red indicates errors or problems - please change your mind using red to indicate a strong password.

In addition, since you repeatedly update a color based on potentially many matches, your colors will not be as consistent as you would like.

Instead, give each of the conditions a rating, 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, @"[az]") && Regex.IsMatch(PassChar, @"[AZ]")) 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; } 

Pay attention to the use of the else-if constructor, which is sometimes simpler than the one supplied using the switch or case language. (C / C ++, in particular, is prone to software errors.)

+2
source share

All Articles