4

I'm currently making a login form to my program where I have a watermark for the two textboxes Email and Password.

When a textbox is empty, its watermark text will appear in it like "Enter email here" and "Enter password here".

My code so far is:

    private void emailLogin_Leave(object sender, EventArgs e){
        if (emailLogin.Text.Length == 0){
            emailLogin.Text = "Email";
            emailLogin.ForeColor = Color.Silver;
        }
    }

    private void emailLogin_Enter(object sender, EventArgs e){
        if (emailLogin.Text == "Email"){
            emailLogin.Text = "";
            emailLogin.ForeColor = Color.Black;
        }
    }

    private void passwordLogin_Leave(object sender, EventArgs e){
        if (passwordLogin.Text.Length == 0){
            passwordLogin.Text = "Password";
            passwordLogin.ForeColor = Color.Silver;
        }
    }

    private void passwordLogin_Enter(object sender, EventArgs e){
        if (passwordLogin.Text == "Password"){
            passwordLogin.Text = "";
            passwordLogin.ForeColor = Color.Black;
        }
    }

But now my problem is that I want to use password characters for the password. But I still want the watermark text to be in regular text. When I check to use a password char it turns my watermark into "**" instead of "Password". How can I fix that?

By the way, I don't want to use the "UseSystemPasswordChar" (those dots). I want to use "PasswordChar" and use asterisks (*) as password chars.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76

1 Answers1

6

Just set it and unset it as you do for the ForeColor :

    private void passwordLogin_Leave(object sender, EventArgs e){
        if (passwordLogin.Text.Length == 0){
            passwordLogin.Text = "Password";
            passwordLogin.ForeColor = Color.Silver;
            passwordLogin.PasswordChar = '\0';
        }
    }

    private void passwordLogin_Enter(object sender, EventArgs e){
        if (passwordLogin.Text == "Password"){
            passwordLogin.Text = "";
            passwordLogin.ForeColor = Color.Black;
            passwordLogin.PasswordChar = '*';
        }
    }
Reda
  • 2,289
  • 17
  • 19
  • Works great! Thank you so much! I did not know about the \0. What exactly does it mean? Again, thank you so much! :) –  Nov 26 '13 at 12:59
  • @user3036459 `\0` is just the null character, i.e. nothing. – Kendall Frey Nov 26 '13 at 13:00
  • So it was that easy, haha! I was trying to figure this out for a good 45 minutes. Thanks! :) –  Nov 26 '13 at 13:01
  • \0 is not really 'nothing' - it's just that the PasswordChar property treats zero as having the special meaning of 'don't display password masking' – Will Dean Nov 26 '13 at 13:02