3

I have a textbox in my WinForm and when I type the password in, its hidden because:

private void textBoxPWMain2_TextChanged(object sender, EventArgs e)
{
    textBoxPWMain2.UseSystemPasswordChar = true;
}

is it possible to add here a button, and while the button is pressed, the password show normal and when I stop pressing the button, the password will hide again?

steffen1994
  • 247
  • 2
  • 12
  • 6
    Possible duplicate of [How can I unmask password text box and mask it back to password?](https://stackoverflow.com/questions/8185747/how-can-i-unmask-password-text-box-and-mask-it-back-to-password) – Alexandre Beaudet Jun 07 '18 at 11:58
  • 3
    @AlexandreBeaudet I don't think the question is about how to switch `UseSystemPasswordChar`, but about how to get notified when a button is _pressed_ and when it is _released_ (in contrast to the whole "click" event) – René Vogt Jun 07 '18 at 12:03
  • @RenéVogt Fair enough, the question wasn't really specific (in my opinion), I'll leave the link for other people that might check this thread for answers – Alexandre Beaudet Jun 07 '18 at 12:06
  • 2
    You may want to take a look at a [TextBox with show password eye icon](https://github.com/r-aghaei/TextBoxWithShowPasswordEyeIcon) – Reza Aghaei Jun 07 '18 at 13:31
  • @RezaAghaei that sample you've posted is really great – Lucifer Jun 07 '18 at 17:47

4 Answers4

9

Maybe this? (Don't forget to subscribe to these events)

private void button2_MouseDown(object sender, EventArgs e)
{
    textBoxPWMain2.UseSystemPasswordChar = false;
}

private void button2_MouseUp(object sender, EventArgs e)
{
    textBoxPWMain2.UseSystemPasswordChar = true;
}
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • you need to change the `PasswordChar to '\0'` to achieve it – Lucifer Jun 07 '18 at 12:21
  • @isprio how do I subscribe to this event? – steffen1994 Jun 07 '18 at 12:42
  • 2
    @steffen1994 you either type `yourButtonNameHere.MouseDown +=` and hit the Tab button, or you select the button in the designer and then click on the lightningbolt icon ⚡ in the properties pane, and then double click `MouseDown`. And the same for `MouseUp`. – ispiro Jun 07 '18 at 12:52
5

I have a solution now, I wanted something like a eye button, when you press it down the password shows, when you stop pressing, the password hides again.

Solution First I added a pictureBox with Eye Icon and added this pictureBox to my password textbox and set Passwort textbox to .UseSystemPasswordChar

public Form1
{
textBoxPW.Controls.Add(pictureBoxEye);
pictureBoxEye.Location = new Point(95,0);
pictureBoxEye.BackColor = Color.Transparent;

textBoxPW.UseSystemPasswordChar = true;

//Subscribe to Event
pictureBoxPW.MouseDown += new MouseEventHandler(pictureBoxPW_MouseDown);
pictureBoxPW.MouseUp += new MouseEventHandler(pictureBoxPW_MouseUp);
}

Added the Mouse_Down/Up Event

private void pictureBoxEye_MouseDown(object sender, MouseEventArgs e)
    {
        textBoxPW.UseSystemPasswordChar = false;

    }

private void pictureBoxEye_MouseUp(object sender, MouseEventArgs e)
    {
        textBoxPW.UseSystemPasswordChar = true;

    }

enter image description here

This works fine for me! Thank you guys !!

steffen1994
  • 247
  • 2
  • 12
3

Adding a bit change details to ispiro's answer

public void button1_MouseDown(object sender, EventArgs e)
{
    textBox1.PasswordChar = '\0';
    textBox1.UseSystemPasswordChar = false;
}

public void button1_MouseUp(object sender, EventArgs e)
{
    textBox1.PasswordChar = '*';
    textBox1.UseSystemPasswordChar = true;
}

Before:- enter image description here

After :- enter image description here

Lucifer
  • 1,594
  • 2
  • 18
  • 32
2

Is there a reason you set the UseSystemPasswordChar in the TextChanged event?

If you can set the property in the Initialize() method or in the constructor you can implement the following events for your button:

private void button1_MouseDown(object sender, MouseEventArgs e)
{
    textBoxPWMain2.UseSystemPasswordChar = false;
}

private void button1_MouseUp(object sender, MouseEventArgs e)
{
    textBoxPWMain2.UseSystemPasswordChar = true;
}
Andy
  • 111
  • 6
  • you need to change the PasswordChar to '\0' to achieve it – Lucifer Jun 07 '18 at 12:21
  • @Lucifer in my example it works without setting the PasswordChar to '\0' (Target framework .NET Framework 4.7.1 Output type: Windows Application) – Andy Jun 07 '18 at 12:29