How can I unmask password text box and mask it bac

2019-01-07 23:58发布

问题:

How can password textbox that set to :

password_txtBox.PasswordChar ="*"

to be unmasked ( from checkbox ) and then mask again
without loosing the string inside the textbox

回答1:

Just set the property to '\0' (which is the default value) to not mask characters.

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

Note: notice that '\0' is different from '0'. The first one is the null character, white '0' is the character that will be displayed as 0.



回答2:

For winforms:

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


回答3:

If you are working with toggle switch then

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

here '\0' will show to password filed to plain text



回答4:

txtPassword is the Password textbox, chkSeePassword is the Show password checkbox. Now add some code to the checkbox's CheckedChanged event

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


回答5:

One of the easiest method to show and hide password is by using radio button inside password text box

The properties of radio button should be like:

this.radioBtn_ShowHidePassword.AutoCheck = false;    

then the clicking activity has to be taken care manually just making it to be reverse of present state in its "Click" event

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

then finally the easiest way of show and hide password

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
}


回答6:

use this one

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


回答7:

The VB.Net version is

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

or

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


标签: c# textbox