小编Min*_*eng的帖子

如何在C#中删除并重新连接EventHandler到控件?

我读过这个答案.它只是告诉我如何从按钮控件中删除click事件.我想知道如何更改代码(特别是GetField("EventClick"...部分!)所以我可以用其他控件做同样的事情.例如,我想删除a的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)
    { …
Run Code Online (Sandbox Code Playgroud)

c# event-handling winforms

5
推荐指数
2
解决办法
5280
查看次数

标签 统计

c# ×1

event-handling ×1

winforms ×1