Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#文本框焦点丢失_C#_Textbox - Fatal编程技术网

C#文本框焦点丢失

C#文本框焦点丢失,c#,textbox,C#,Textbox,我已经为文本设置了标准文本和颜色,当单击文本框时,我清除用户类型文本的文本并定义黑色。 点击事件: if (txtbox.Text == "Ex.: [text test]") { txtbox.Text = string.Empty; txtbox.ForeColor = Color.Black; } 如果文本框为空且焦点位于另一个文本框中,我想设置默认文本,因此如果用户单击或按

我已经为文本设置了标准文本和颜色,当单击文本框时,我清除用户类型文本的文本并定义黑色。 点击事件:

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

如果文本框为空且焦点位于另一个文本框中,我想设置默认文本,因此如果用户单击或按tab键。

您可以在
离开
事件中设置默认文本。这将在文本框失去焦点时运行

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

请在click和keypress事件函数中编写以下代码(用于选项卡的工作)


如果表单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";
   }
}

我想可以。如果发生任何错误,请告诉我。

您使用的是WPF还是Winforms?
  private void textBox1_Validating(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(textBox1.Text))
        {
            //Your logic here or the color you want 
        }
    }
  private void textBox1_Validating(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(textBox1.Text))
        {
            //Your logic here or the color you want 
        }
    }