我读过这个answer https://stackoverflow.com/questions/91778/how-to-remove-all-event-handlers-from-a-control。
它只是告诉我如何从按钮控件中删除单击事件。我想知道如何更改代码(尤其是GetField("EventClick"...
部分!)这样我就可以用其他控件做同样的事情。例如,我想删除TextChanged
的事件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 将具有类似 placeholder 的功能。也许您可以给我一些有关如何将占位符添加到文本框的帮助。这就是我想要的。
Remove
Mybutton.event -= methodname;
Reattach
Mybutton.event += methodname;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)