user3745020
user3745020

Reputation: 81

Display Password from the text Box

I have made a TextBox Control with the PasswordChar = '*'; Now I want to show the password if user checks a checkbox. Strangely it is not working and I am not able to see my passsword

Here is my code

if (DisplayPasswordCheckBox.Checked)
     PasswordTB.PasswordChar = char.Parse("\0");
else
     PasswordTB.PasswordChar = char.Parse("*");

Any idea what I am doing wrong here ?

Edited : If you are using UseSystemPasswordChar = true, turn that off before changing the password char to get the result on screen

Upvotes: 3

Views: 839

Answers (3)

user2160375
user2160375

Reputation:

From msdn:

The character used to mask characters entered in a single-line TextBox control. Set the value of this property to 0 (character value) if you do not want the control to mask characters as they are typed. Equals 0 (character value) by default.

Source: msdn

So the solution is

if (DisplayPasswordCheckBox.Checked)
     PasswordTB.PasswordChar = '\0'; //msdn says: 0 as character value.
else
     PasswordTB.PasswordChar = '*';

Other solution:

PasswordTB.PasswordChar = (char)0;

Upvotes: 2

Christos
Christos

Reputation: 53958

You should change you code to the following one:

if (DisplayPasswordCheckBox.Checked)
    PasswordTB.PasswordChar = '\0';
else
    PasswordTB.PasswordChar = '*';

You don't have to parse anything. So you don't need to use char.Parse() method.

For more information about the property called PasswordChar, please have a look here.

Upvotes: 4

Md.Rajibul Ahsan
Md.Rajibul Ahsan

Reputation: 853

Try this hopefully it will help

    if (DisplayPasswordCheckBox.Checked)
        PasswordTB.PasswordChar = '\0';
    else
        PasswordTB.PasswordChar = char.Parse("*");

Upvotes: 0

Related Questions