簡體   English   中英

C#失去對文本框的關注

[英]C# loss of focus on textbox

我已經為文本設置了標准文本和顏色,單擊文本框后,我清除了用戶類型文本的文本並定義了黑色。 點擊事件:

 if (txtbox.Text == "Ex.: [text test]")
            {
                txtbox.Text = string.Empty;
                txtbox.ForeColor = Color.Black;
            }

如果文本框為空並且焦點位於另一個文本框中,那么我想設置默認文本,因此如果用戶單擊或按下選項卡。

  private void textBox1_Validating(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(textBox1.Text))
        {
            //Your logic here or the color you want 
        }
    }

您可以在Leave事件中設置默認文本。 每當文本框失去焦點時,它將運行。

    private void textBox1_Leave(object sender, EventArgs e)
    {
        if (textBox1.Text == String.Empty)
        {
            //Set default text here.  
        }
    }

請同時在click和keypress事件功能中寫入以下代碼(用於標簽的工作)

If(txtbox.Text == "")
{
    txtbox.Text = "Default";
    txtbox1.focus();  //focus will be set to another textbox.
}

如果表單form1上有兩個文本框txt1和txt2,則

form1_Load(object sender, System.EventArgs e)
{
    txt2.SetFocus;
    txt1.text = "Default Text";
}
txt1_Click(object sender, System.EventArgs e)
{
    if(txt1.text == "Default Text")
    {
        txt1.text = "";
    }
}
txt1_Leave(object sender, System.EventArgs e)
{
   if(txt1.text == "")
   {
        txt1.text = "Default Text";
   }
}

我認為這會起作用。如果發生任何錯誤,請通知我。

暫無
暫無

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

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