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

时间:2014-04-03 22:59:13

标签: c# winforms controls

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

我现在所拥有的:

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

3 个答案:

答案 0 :(得分:3)

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

txtPassword.PasswordChar = '\0';

MSDN

上指定的默认值

答案 1 :(得分:2)

没关系,只是弄明白了:

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

答案 2 :(得分:0)

也许使用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";
        }
    }