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

ste*_*994 2 c# winforms

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

private void textBoxPWMain2_TextChanged(object sender, EventArgs e)
{
    textBoxPWMain2.UseSystemPasswordChar = true;
}
Run Code Online (Sandbox Code Playgroud)

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

isp*_*iro 6

也许这个?(不要忘记订阅这些事件)

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

private void button2_MouseUp(object sender, EventArgs e)
{
    textBoxPWMain2.UseSystemPasswordChar = true;
}
Run Code Online (Sandbox Code Playgroud)

  • @steffen1994 您可以输入 `yourButtonNameHere.MouseDown +=` 并点击 Tab 按钮,或者在设计器中选择按钮,然后单击属性窗格中的闪电图标 ⚡,然后双击 `MouseDown`。“MouseUp”也是如此。 (2认同)

ste*_*994 6

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

解决方案 首先,我添加了一个带有眼睛图标的图片框,并将此图片框添加到我的密码文本框中,并将 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);
}
Run Code Online (Sandbox Code Playgroud)

添加了 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;

    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这对我来说效果很好!感谢你们 !!