Subhankar
Subhankar

Reputation: 45

Custom Password Character in TextBox

I have a TextBox containing a password that I want to be able to show or hide depending on the value of a CheckBox. I am able to hide the characters by setting the UseSystemPasswordChar property in the CheckChanged event on chkBox:

private void chkBox_CheckedChanged(object sender, EventArgs e)
{
    if (chkBox.Checked)
    {
        txtBox.UseSystemPasswordChar = false;
    }
    else
    {
        txtBox.UseSystemPasswordChar = true;
    }

}

I would like to use a custom character to replace the password text instead of the system character. How can I use a custom password character?

Upvotes: 3

Views: 466

Answers (2)

Roman Kovalchuk
Roman Kovalchuk

Reputation: 53

textBox1.PasswordChar = '$';

This code is ignored if property "UseSystemPasswordChar = true", change it to "false" and it will work.

Upvotes: 0

Alireza
Alireza

Reputation: 5503

I guess what you want can be done with:

 txtBox.PasswordChar = '$';

or more specifically

txtBox.PasswordChar = chkBox.Checked ? Char.MinValue : '$';

Upvotes: 3

Related Questions