22

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

RBT
  • 24,161
  • 21
  • 159
  • 240
user63898
  • 29,839
  • 85
  • 272
  • 514
  • 8
    Winforms, WPF, or ASP.NET? – David Nov 18 '11 at 16:40
  • 4
    Metro? WinForms? WPF? Silverlight? ASP.Net? MonoTouch? – SLaks Nov 18 '11 at 16:42
  • Please at least read the documentation before you ask questions like that. as pointed out by @Renaud, it's in the first paragraph of the MSDN Documentation. – Eric Liprandi Nov 18 '11 at 16:49
  • 13
    @EricLiprandi: Personally, I don't see any problem with asking this question. Maybe it was right there in front of you because you knew where to look? The ability to find the information that you're looking for is something that comes with experience, and not everyone is at the same level. – James Johnson Nov 18 '11 at 16:53
  • **Related posts** - [showing password characters on some event for passwordbox](https://stackoverflow.com/q/10091466/465053), [A good way to show password in PasswordBox](https://stackoverflow.com/q/17007630/465053), **&** [Where can I find a free masked TextBox in WPF?](https://stackoverflow.com/q/481059/465053) – RBT Apr 10 '18 at 04:53
  • [TextBox with show password eye icon](https://github.com/r-aghaei/TextBoxWithShowPasswordEyeIcon) – Reza Aghaei Jun 07 '18 at 13:35

9 Answers9

28

For winforms:

private void checkBoxShowPassword_CheckedChanged(object sender, EventArgs e) {
   textBoxPassword.PasswordChar = checkBoxShowPassword.Checked ? '\0' : '*';
}
Otiel
  • 18,404
  • 16
  • 78
  • 126
  • "PasswordChar" is used in winform application how to din in asp.net with C# – Sunil Acharya Oct 24 '15 at 11:48
  • 3
    To match the character that is used to mask passwords in Windows, use `●` or any of these if you're feeling fancy: `○` `◌`‬ `●` `◯` `❍` `✪` – c00000fd Sep 14 '17 at 06:52
27

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.

Renaud Dumont
  • 1,776
  • 13
  • 24
  • 1
    The documentation is a little non-specific. It might be helpful to clarify that by "character value", they mean '\0', not '0'. Hence: password_txtBox.PasswordChar = '\0'. Otherwise, you get get zeros as the masking character. – Jamesckel Nov 24 '18 at 17:25
7

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

Adiii
  • 54,482
  • 7
  • 145
  • 148
2

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
}
Garuda prasad K
  • 332
  • 3
  • 8
2

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 = '*';
    }
1

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;
}
123iamking
  • 2,387
  • 4
  • 36
  • 56
1
if(TokenTextBox.PasswordChar == (char)0)
{
   TokenTextBox.PasswordChar = '•';
}
else
{
   TokenTextBox.PasswordChar = (char)0;
}
janw
  • 8,758
  • 11
  • 40
  • 62
NotNazuh
  • 11
  • 2
0

use this one

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

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