Winform中的空密码字符

我在ac#windows窗体中有一个textbox我在为PasswordChar分配空值时遇到问题。 我想要做的是,如果checkbox ,那么PasswordChar应为null即应显示实际文本,否则PasswordChar应为* 。 这就是我所尝试的

  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'; } 

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

 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; } 

您是否尝试阅读TextBox.PasswordChar的手册?

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