C# 登录屏幕的隐藏密码 - 帮助和想法

Hidden Password for C# Login Screen - Help & Ideas

我想在文本框中隐藏密码。密码是硬编码的,我知道这不是很好,但这是项目所需的全部内容。

目前我正在使用点击事件,但是当使用选项卡时,代码没有实现。

如何做到这一点?这是我当前的代码:

// Password Text Box Click Event

private void txtPassword_Click(object sender, EventArgs e)

{

// When password text box clixked, the watermark will disappear

txtPassword.Text = "";

txtPassword.ForeColor = Color.Black;

txtPassword.PasswordChar = '●'; // Password masking for added security

}

我认为使用 Tab 键时不执行该代码的原因是因为它是一个 Click 事件处理程序。尝试改用 Enter 事件,以便在文本框获得焦点时执行您的代码。

编辑:

private void txtPassword_Enter(object sender, EventArgs e)
    {
        txtPassword.Text = "";

        txtPassword.ForeColor = Color.Black;

        txtPassword.UseSystemPasswordChar = true;
    }

    private void txtPassword_Leave(object sender, EventArgs e)
    {
        if (txtPassword.Text.Length == 0)
        {
            txtPassword.ForeColor = Color.Gray;

            txtPassword.Text = "Enter password";

            txtPassword.UseSystemPasswordChar = false;

            SelectNextControl(txtPassword, true, true, false, true);
        }
    }

只需在文本框上使用 UseSystemPasswordChar 属性。将其设置为真。如需更多自定义,请将 PasswordChar 属性 设置为类似 * 或其他一些普遍接受的密码字符。

您可能正在寻找 GotFocus 方法,请尝试以下操作:

this.txtPassword.GotFocus += OnFocus;

private void OnFocus(object sender, EventArgs e)
{
   txtPassword.useSystemPasswordChar = true;
}