添加/删除处理程序的文本框文本框、程序

由网友(回不去的时光)分享简介:我使用下面的code添加处理程序文本框:I am adding a handler to textbox using the following code:private void frmLogin_Load(object sender, EventArgs e){foreach (Control tb in t...

我使用下面的code添加处理程序文本框:

I am adding a handler to textbox using the following code:

private void frmLogin_Load(object sender, EventArgs e)
{
    foreach (Control tb in this.Controls)
    {
        if (tb is TextBox)
        {
            TextBox tb1 = (TextBox)tb;
            tb1.KeyDown += new KeyEventHandler(TextBox_KeyDown);
        }      
    }
}

我也删除处理程序使用下面的code:

I am also removing handler using the following code:

private void frmLogin_FormClosed(object sender, FormClosedEventArgs e)
{
    foreach (Control tb in this.Controls)
    {
        if (tb is TextBox)
        {
            TextBox tb1 = (TextBox)tb;
            tb1.KeyDown -= new KeyEventHandler(TextBox_KeyDown);
        }
    }
}

是正确的做法还是有一个更好的选择?

Is the correct way or is there a better alternative?

推荐答案

这是件好事,但你不需要移除处理程序,并添加处理程序只是把这样的:

It is good, but you dont need to remove the handler, and adding the handler just put this:

tb1.KeyDown += TextBox_KeyDown;

由于新KeyEventHandler(TextBox_KeyDown); 是多余的。

阅读全文

相关推荐

最新文章