簡體   English   中英

C#登錄屏幕的隱藏密碼-幫助和想法

[英]Hidden Password for C# Login Screen - Help & Ideas

我想在文本框中隱藏密碼。 密碼是硬編碼的,我知道這不是很好,但是是項目所需的全部。

目前,我正在使用click事件,但是當使用tab時並沒有實現該代碼。

如何才能做到這一點? 這是我當前的代碼:

// 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

}

只需在文本框中使用UseSystemPasswordChar屬性即可。 將其設置為true。 要進行更多自定義,請設置PasswordChar屬性,使之類似於*或其他一些常用的密碼字符。

我相信使用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);
        }
    }

您可能正在尋找GotFocus方法,請嘗試以下操作:

this.txtPassword.GotFocus += OnFocus;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM