user63898
user63898

Reputation: 30885

How can I unmask password text box and mask it back to password?

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

Upvotes: 24

Views: 85950

Answers (9)

NotNazuh
NotNazuh

Reputation: 21

if(TokenTextBox.PasswordChar == (char)0)
{
   TokenTextBox.PasswordChar = '•';
}
else
{
   TokenTextBox.PasswordChar = (char)0;
}

Upvotes: 2

Jurgen Volders
Jurgen Volders

Reputation: 116

I did this to briefly show the password (I am pretty new at coding so feedback welcome if this is a bad practise)

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

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

Upvotes: 2

Renaud Dumont
Renaud Dumont

Reputation: 1806

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.

Upvotes: 30

Garuda prasad K
Garuda prasad K

Reputation: 352

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
}

Upvotes: 2

Adiii
Adiii

Reputation: 59916

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

Upvotes: 7

Sumit Saha
Sumit Saha

Reputation: 1

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

Upvotes: 0

Amir
Amir

Reputation: 1

use this one

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

Upvotes: 0

123iamking
123iamking

Reputation: 2703

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

Upvotes: 2

Otiel
Otiel

Reputation: 18743

For winforms:

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

Upvotes: 29

Related Questions