1

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#?

CSDev
  • 3,177
  • 6
  • 19
  • 37
minaxi
  • 35
  • 6

3 Answers3

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

enter image description here

CSDev
  • 3,177
  • 6
  • 19
  • 37
1

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

BPDESILVA
  • 2,040
  • 5
  • 15
  • 35
0

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 = '*';
Mofid.Moghimi
  • 907
  • 1
  • 6
  • 13