如何取消屏蔽密码文本框并将其掩盖回密码?

时间:2011-11-18 16:38:51

标签: c# textbox

密码文本框如何设置为:

password_txtBox.PasswordChar ="*"

要取消屏蔽(从复选框)然后再次屏蔽
没有丢失文本框内的字符串

8 个答案:

答案 0 :(得分:22)

winforms

private void checkBoxShowPassword_CheckedChanged(object sender, EventArgs e) {
   textBoxPassword.PasswordChar = checkBoxShowPassword.Checked ? '\0' : '*';
}

答案 1 :(得分:16)

只需将属性设置为'\ 0'(这是默认值)即不屏蔽字符。

来源:http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.passwordchar.aspx

注意:请注意'\ 0'与'0'不同。第一个是空字符,白色'0'是将显示为0的字符。

答案 2 :(得分:4)

如果您正在使用拨动开关,那么

private void toggleSwitch1_Toggled(object sender, EventArgs e)
{
    if (toggleSwitch1.IsOn)
    {
        string a= textBox2.Text;
        textBox2.PasswordChar = '\0';
    }
    else
    {
        textBox2.PasswordChar = '*';
    }
}

这里'\0'将显示密码提交到纯文本

答案 3 :(得分:1)

txtPassword是密码文本框,chkSeePassword是显示密码复选框。现在在复选框的CheckedChanged事件中添加一些代码

private void chkSeePassword_CheckedChanged(object sender, EventArgs e)
{
        txtPassword.UseSystemPasswordChar = !chkSeePassword.Checked;
}

答案 4 :(得分:1)

显示和隐藏密码的最简单方法之一是使用密码文本框内的单选按钮

单选按钮的属性应类似于:

this.radioBtn_ShowHidePassword.AutoCheck = false;    

然后点击操作必须手动进行,以使其在“点击”事件中恢复当前状态

private void radioBtn_ShowHidePassword_Click(object sender, EventArgs e)
{
 radioBtn_ShowHidePassword.Checked = (! radioBtn_ShowHidePassword.Checked);
}

然后是显示和隐藏密码的最简单方法

private void radioBtn_ShowHidePassword_CheckedChanged(object sender, EventArgs e)
{
   txtBoxPassword.PasswordChar = radioBtn_ShowHidePassword.Checked ? '\0' : '*';
   // here we can even include the code for changing the default picture of button to two different
   //representation like closed eye and opened eye which resembles Windows login
}

答案 5 :(得分:0)

使用这个

private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        textBox2.PasswordChar = default(char);
    }

答案 6 :(得分:0)

VB.Net版本

Private Sub checkBoxShowPassword_CheckedChanged(sender As Object, e As System.EventArgs) Handles checkBoxShowPassword.CheckedChanged
    textBoxPassword.PasswordChar = If(checkBoxShowPassword.Checked, ControlChars.NullChar, "*"C)
End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked Then
        Me.txt_password.PasswordChar = "*"c
    Else
        Me.txt_password.PasswordChar = ControlChars.NullChar
    End If
End Sub

答案 7 :(得分:0)

我这样做是为了简要显示密码 (我在编码方面很新,所以如果这是一种不好的做法,欢迎反馈)

 private void buttonShowPassword_MouseDown(object sender, MouseEventArgs e)
    {
        TextBoxPassword.Properties.PasswordChar =(char)0;
    }

    private void buttonShowPassword_MouseUp(object sender, MouseEventArgs e)
    {
        TextBoxPassword.Properties.PasswordChar = '*';
    }