按住按钮在文本框中显示密码

时间:2018-06-07 11:55:46

标签: c# winforms

我的WinForm中有一个文本框,当我输入密码时,隐藏了因为:

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

是否可以在这里添加一个按钮,按下按钮时,密码显示正常,当我停止按下按钮时,密码会再次隐藏?

4 个答案:

答案 0 :(得分:2)

也许这个? (别忘了订阅这些活动)

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

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

答案 1 :(得分:2)

您是否有理由在TextChanged事件中设置UseSystemPasswordChar?

如果您可以在Initialize()方法或构造函数中设置属性,则可以为按钮实现以下事件:

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

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

答案 2 :(得分:1)

将位更改详细信息添加到ispiro's回答

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;
}

在: - enter image description here

之后: - enter image description here

答案 3 :(得分:0)

我现在有一个解决方案,我想要一个像眼睛按钮的东西,当你按下密码显示时,当你停止按下时,密码会再次隐藏。

<强>解决方案  首先,我添加了一个带有Eye Icon的pictureBox,并将此pictureBox添加到我的密码文本框中,并将Passwort文本框设置为.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);
}

添加了Mouse_Down / Up事件

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

这对我来说很好!谢谢你们!!