7

我有一个textboxac# windows 窗体,我在将空值分配给PasswordChar. 我想要做的是,如果 acheckbox被选中,那么PasswordChar应该是null,即应该显示实际的文本,否则PasswordChar应该是*。这是我尝试过的

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

但是这条线

     txtPassword.PasswordChar = ''; 

正在产生错误。我什至尝试过

     txtPassword.PasswordChar = null;

但我仍然得到一个错误。

请帮我更正我的代码。

4

4 回答 4

18

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

为了您的方便:

private void checkBox1_CheckedChanged(object sender, EventArgs e){
   txtPassword.PasswordChar = checkBox1.Checked ? '*' : '\0';
}
于 2013-07-23T11:15:28.683 回答
1

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

textBox1.PasswordChar = (char)0;

或这个

textBox1.PasswordChar = '\0';
于 2014-02-16T15:27:11.767 回答
1

有关其他信息:

里面有一个替代方案TextBox.PasswordChar,你也可以用TextBox.UseSystemPasswordChar

private void checkBox1_CheckedChanged(object sender, EventArgs e){
   textBox1.UseSystemPasswordChar = checkBox1.Checked ? true : false;
}
于 2014-11-14T05:59:18.963 回答
0

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

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

于 2013-07-23T11:18:34.603 回答