我读过这个答案。它只是告诉我如何从按钮控件中删除单击事件。我想知道如何更改代码(尤其是GetField("EventClick"...
部分!),以便我可以对其他控件做同样的事情。例如,我想删除TextChanged
a 的事件TextBox
。而且我还想知道如何重新附加事件处理程序。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Length < 10) return;
MessageBox.Show("do something");
}
private void Form1_Load(object sender, EventArgs e)
{
Tools.mkTextBoxWithPlaceholder(textBox1, "hi, input here...");
}
}
class Tools
{
public static void mkTextBoxWithPlaceholder(TextBox tb, string placeholder)
{
tb.Tag = placeholder;
tb.GotFocus += new EventHandler(tb_GotFocus);
tb.LostFocus += new EventHandler(tb_LostFocus);
}
private static void tb_GotFocus(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
tb.Clear();
}
private static void tb_LostFocus(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
//TODO Remove the TextChanged event handler here.
tb.Text = tb.Tag as string;
//TODO Reattach the TextChanged event handler here.
}
}
使用上面的代码,textBox1 将具有类似占位符的功能。也许您可以就如何将占位符添加到文本框给我一些帮助。这就是我想要的。