Home > other >  How to change label text and color when a condition is met? (C#)
How to change label text and color when a condition is met? (C#)

Time:01-25

I am making a password generator and on websites when you enter certain conditions are met the strength of the password changes how can I change the color and text of the label when the password strength is >= 8, <8<10, >12?

Here is the Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Password_Generator
{
    public partial class PassGen : Form
    {

        int currentPasswordLength = 0;
        Random Character = new Random();
        


        private void PasswordGenerator(int PasswordLength)
        {
            String validChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$&?";
            String randomPassword = "";
            //25
            for(int i = 0; i < PasswordLength; i  )
            {
                int randomNum = Character.Next(0, validChars.Length);
                randomPassword  = validChars[randomNum];
            }
            Password.Text = randomPassword;
        }

        public PassGen()
        {
            InitializeComponent();
            PasswordLengthSlider.Minimum = 5;
            PasswordLengthSlider.Maximum = 22;
            PasswordGenerator(5);

        }

        private void Label1_Click(object sender, EventArgs e)
        {

        }

        private void Copy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(Password.Text);
        }
        //52
        private void PasswordLength_Click(object sender, EventArgs e)
        {

        }

        private void PasswordLengthSlider_Scroll(object sender, EventArgs e)
        {
            PasswordLength.Text = "Password Length:"   " "   PasswordLengthSlider.Value.ToString();
            currentPasswordLength = PasswordLengthSlider.Value;
            PasswordGenerator(currentPasswordLength);

        }

        private void pswdStrengthTest()
        {
            if (currentPasswordLength <= 8)
            {
                pswdStrength.Text = "weak";
                pswdStrength.ForeColor = Color.Red;
            } else if (currentPasswordLength<= 9)
            {
                pswdStrength.Text = "ok";
                pswdStrength.ForeColor = Color.Blue;
            }
        }
        //78
        private void pswdStrength_Click(object sender, EventArgs e)
        {

        }
    }
}

If anyone could help me with this it would be greatly appreciated. This is based off a tutorial I found on YouTube. I'm not sure what the video is called but if it helps I could search for it and update my posting.

CodePudding user response:

Try this:

Password.TextChanged  = (s1, e1) =>
{
    if (Password.Text.Length > 10)
        pswdStrength.ForeColor = Color.Green
    else if (Password.Text.Length > 8)
        pswdStrength.ForeColor = Color.Blue
    else
        pswdStrength.ForeColor = Color.Red
};

CodePudding user response:

Your code looks like a windows form application.

If you have for example one objetc txt_password, check to code some of these events:

  • TextChanged: this occurs when your textbox has been changed enter image description here

Others events could be:

  • KeyPress or KeyDown
  •  Tags:  
  • Related