minaxi
minaxi

Reputation: 35

How to make password text box value visible when the cursor is over an icon in winform

I am creating a winforms application using C#. Now I want to add a small eye Icon next to the password textbox so when the user hovers over the Icon, one can see what is entered so far. So while hovering, the textbox should show 123 and when the user leaves the icon the box should be shown as *** again . Is there any way to do this with C#?

Upvotes: 1

Views: 1424

Answers (3)

CSDev
CSDev

Reputation: 3235

textBox.UseSystemPasswordChar = true;
icon.MouseEnter += (sender, e) => textBox.UseSystemPasswordChar = false;
icon.MouseLeave += (sender, e) => textBox.UseSystemPasswordChar = true;

enter image description here

Upvotes: 5

BPDESILVA
BPDESILVA

Reputation: 2198

On the hover event use :

Recommended - use an if condition to validate if hover called & set UseSystemPasswordChar property to true or false based on the action.

//hover condition
if() {   
   textBox1.UseSystemPasswordChar = False;
} else {
   textBox1.UseSystemPasswordChar = True;
}

Or

textBox1.PasswordChar = '\0';

The UseSystemPasswordChar property has precedence over the PasswordChar property. Whenever the UseSystemPasswordChar is set to true, the default system password character is used and any character set by PasswordChar is ignored. - Source

More Solutions could be found at here

Official Documentation .Net Framework 4.8:

UseSystemPasswordChar - This Link

PasswordChar - This link (Not masked textbox)

MaskedTextBox.PasswordChar - This link

Upvotes: 1

Mofid.Moghimi
Mofid.Moghimi

Reputation: 1005

In hover event on icon, you need change PasswordChar property of TextBox to char.MinValue like this:

textBox1.PasswordChar = char.MinValue;

And then if user leave icon, change this property again

textBox1.PasswordChar = '*';

Upvotes: 0

Related Questions