在文本框中打开和关闭密码行为

本文关键字:密码 文本 | 更新日期: 2023-09-27 17:56:44

我有一个文本框,我将其用作密码框,我想让默认密码可见,然后在更改时,用'*'将其隐藏。因此,当用户打开表单时,"密码"文本框中已经有"welcome1",但是当他们更改它时,密码将显示为"*"。

我目前拥有:

if (txtPassword.Text == "welcome1")
{
    // Set txtPassword.PasswordChar to null or empty.
}
else { txtPassword.PasswordChar = '*'; }

在文本框中打开和关闭密码行为

要将密码字符重置为不屏蔽,请将其设置为空字符:

txtPassword.PasswordChar = ''0';

在 MSDN 上指定的默认值

关系,刚刚想通了:

private void txtPassword_TextChanged(object sender, EventArgs e)
{
    if (txtPassword.Text != "welcome1")
    {
        txtPassword.PasswordChar = '*';
    }
}

也许使用 2 个文本框。 1e 可见真 2e 可见假并将 2e 文本框密码字符设置为 *在第一个文本框上单击鼠标事件。当有人单击文本框以键入密码时,文本框会更改。

    private void textBox1_MouseClick(object sender, MouseEventArgs e)
    {
        textBox1.Text = "";
        textBox1.Visible = false;
        textBox2.Visible = true;
        textBox2.Focus();
    }

还放一个表单鼠标点击,如果你没有输入文本,回到文本框1或类似的东西。

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        if (textBox2.Text == "")
        {
            textBox2.Visible = false;
            textBox1.Visible = true;
            textBox1.Text = "Welcome1";
        }
    }