添加/删除处理程序的文本框 [英] Add/Remove handler to textbox

查看:162
本文介绍了添加/删除处理程序的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的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); 是多余的。

这篇关于添加/删除处理程序的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆