C# Winform中的空密码字符

C# Winform中的空密码字符,c#,winforms,textbox,C#,Winforms,Textbox,我在c#windows窗体中有一个文本框,我在为密码字符分配空值时遇到问题。我想做的是,如果选中复选框,则密码字符应为null,即实际文本应显示,否则密码字符应为*。这就是我尝试过的 private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (!checkBox1.Checked) { txtPassword.PasswordChar = '

我在c#windows窗体中有一个
文本框
,我在为
密码字符
分配空值时遇到问题。我想做的是,如果选中
复选框
,则
密码字符
应为
null
,即实际文本应显示,否则
密码字符
应为
*
。这就是我尝试过的

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

     txtPassword.PasswordChar = ''; 
正在生成错误。我甚至试过

     txtPassword.PasswordChar = null;
但我还是犯了一个错误


请帮我更正代码。

要重置
PassswordChar
,请执行此操作
txtPassword.PasswordChar='\0'

为方便起见:

private void checkBox1_CheckedChanged(object sender, EventArgs e){
   txtPassword.PasswordChar = checkBox1.Checked ? '*' : '\0';
}

你有没有试着读这本手册

如果不希望控件在键入字符时屏蔽字符,请将此属性的值设置为0(字符值)


使用此代码设置空密码字符

textBox1.PasswordChar = (char)0;
还是这个

textBox1.PasswordChar = '\0';

有关其他信息:

TextBox.PasswordChar
中有一个替代选项,您也可以使用
TextBox.UseSystemPasswordChar

private void checkBox1_CheckedChanged(object sender, EventArgs e){
   textBox1.UseSystemPasswordChar = checkBox1.Checked ? true : false;
}

谢谢,非常好,非常好,谢谢