C#占位符文本框+ UseSystemPasswordChar

时间:2019-03-08 19:13:15

标签: c# winforms checkbox placeholder

我正在使用Visual Studio 2015,我正在寻找有关我的想法的技巧。所以我有一个带有占位符文本“ Password”的文本框,当我关注它时,它的前景色会改变,并开始使用UseSystemPasswordChar = true。我有一个用于占位符文本的类:

class PlaceHolderTextBox
{
    public void SetPlaceHolder(Control control, string PlaceHolderText)
    {
        control.Text = PlaceHolderText;
        control.GotFocus += delegate (object sender, EventArgs args) {
            if (control.Text == PlaceHolderText)
            {
                control.Text = "";
                control.ForeColor = Color.Black;
            }

        };
        control.LostFocus += delegate (object sender, EventArgs args) {
            if (control.Text.Length == 0)
            {
                control.Text = PlaceHolderText;
                control.ForeColor = SystemColors.InactiveCaption;
            }

        };
    }
}

并且我需要一个复选框“ Show password”,如果选中,则允许显示密码。这是我的登录代码:

private void Login_Load(object sender, EventArgs e)
    {
        PlaceHolderTextBox tb = new PlaceHolderTextBox();
        tb.SetPlaceHolder(TbUsername, "Username");
        tb.SetPlaceHolder(TbPassword, "Password");
    }

    private void LlSignUp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        SignUp reg = new SignUp();
        reg.Show();
    }

    private void LlClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        Application.Exit();
    }

    private void BtLogin_Click(object sender, EventArgs e)
    {
        this.Hide();
        Main main = new Main();
        main.Show();
    }

    private void CbShowPassword_CheckedChanged(object sender, EventArgs e)
    {
        if (CbShowPassword.Checked)
            TbPassword.UseSystemPasswordChar = false;
        else
            TbPassword.UseSystemPasswordChar = true;

    }

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

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

问题是我不能同时使用这两种解决方案,也不知道如何解决

0 个答案:

没有答案