C# textbox 先显示文字 单击后文字消失 急!!!

如题所述

第1个回答  2020-06-18
可以用 控制元件 GotFocus 与 LostFoucs 事件
string default_crtl_txt = "Enter Msg";
假设控制名称为 textBox1,进行事件方法委托
textBox1.GotFocus += new EventHandler(textBox1_GoFocus); textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
private void textBox1_GoFocus(object sender, EventArgs e)
{
if(textBox1.Text == default_crtl_txt )
textBox1.Text = string.Empty;
}

private void textBox1_LostFocus(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
textBox1.Text = "Enter Message";
}
相似回答