Null Password Char in Winform [duplicate]

2019-01-15 14:11发布

This question already has an answer here:

I have a textbox in a c# windows form i am having problems in assigning a null values to a PasswordChar. What i want to do is that if a checkbox is checked then the PasswordChar should be null i.e the actual text should be displayed else the PasswordChar should be *. This what i have tried

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (!checkBox1.Checked)
        {
            txtPassword.PasswordChar = '*';
        }
        else
        {
            txtPassword.PasswordChar = '';
        }
    }

but this line

     txtPassword.PasswordChar = ''; 

is generating an error. I have even tried

     txtPassword.PasswordChar = null;

but i still get an error.

Please help me correct my code.

4条回答
一夜七次
2楼-- · 2019-01-15 14:49

using this code to set null Password character

textBox1.PasswordChar = (char)0;

or this

textBox1.PasswordChar = '\0';
查看更多
Evening l夕情丶
3楼-- · 2019-01-15 14:52

Did you try reading the manual for TextBox.PasswordChar?

Set the value of this property to 0 (character value) if you do not want the control to mask characters as they are typed.

查看更多
贪生不怕死
4楼-- · 2019-01-15 15:00

For additional infomation:

There is an alternative in TextBox.PasswordChar, you can also use TextBox.UseSystemPasswordChar.

private void checkBox1_CheckedChanged(object sender, EventArgs e){
   textBox1.UseSystemPasswordChar = checkBox1.Checked ? true : false;
}
查看更多
成全新的幸福
5楼-- · 2019-01-15 15:08

To reset PassswordChar, do this txtPassword.PasswordChar = '\0';

For your convenience:

private void checkBox1_CheckedChanged(object sender, EventArgs e){
   txtPassword.PasswordChar = checkBox1.Checked ? '*' : '\0';
}
查看更多
登录 后发表回答