在C#中重用事件处理好的做法 [英] Reuse event handler good practice in C#

查看:142
本文介绍了在C#中重用事件处理好的做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我目前的项目中,我使用在不同的标签四个栅格意见。随着系统的发展,他们有一些共同的方法,如显示自定义工具提示和右键菜单上行时。

我现在正在经历一个code清洗运动。我看到下面是我现在有四个事件处理函数调用同样的方法。是否确定所有改变的事件处理程序直接指向GridMenu,避免了额外的code?这会不会导致我的问题在后面的发展?

显然,在present我使用的是默认的,即使处理程序名称。

 私人无效grdEnquiriesLevel1_ShowGridMenu(对象发件人,GridMenuEventArgs E)
    {
        GridMenu(发件人,E);
    }

    私人无效grdApplicantsLevel1_ShowGridMenu(对象发件人,GridMenuEventArgs E)
    {
        GridMenu(发件人,E);
    }

    私人无效grdApplicationsLevel1_ShowGridMenu(对象发件人,GridMenuEventArgs E)
    {
        GridMenu(发件人,E);
    }

    私人无效grdInterviewsLevel1_ShowGridMenu(对象发件人,GridMenuEventArgs E)
    {
        GridMenu(发件人,E);
    }

    私人无效GridMenu(对象发件人,GridMenuEventArgs E)
    {
        GridView的视图=(GridView的)发件人;

        如果(view.CalcHitInfo(e.Point).InRow)
            popupMenu1.ShowPopup(Cursor.Position);
    }
 

解决方案

而不是直接注册到 GridMenu 中,创建一个名为一般的事件处理程序 Grid_ShowGridMenu

只需注册,而不是创建每格一个单独的事件处理程序相同的事件处理程序,每个网格。

  grdEnquiriesLevel1.ShowGridMenu + = Grid_ShowGridMenu;
grdApplicantsLevel1.ShowGridMenu + = Grid_ShowGridMenu;
grdApplicationsLevel1.ShowGridMenu + = Grid_ShowGridMenu;
grdInterviewsLevel1.ShowGridMenu + = Grid_ShowGridMenu;


私人无效Grid_ShowGridMenu(对象发件人,GridMenuEventArgs E)
{
    GridMenu((GridView的)发件人,e.Point);
}
 

现在,而不是传球,发件人,E 直接 GridMenu ,只有通过的需要 GridMenu 并更改 GridMenu 的签名,以便它可以更加的可重复使用的<​​/ em>的。

 私人无效GridMenu(GridView的网格,点Hitpoint的)
{
    如果(grid.CalcHitInfo(Hitpoint的).InRow)
        popupMenu1.ShowPopup(Cursor.Position);
}
 

In my current project, I am using four grid views in different tabs. As the system has developed, they have some shared methods, such as show a custom tooltip and a right click menu for when on rows.

I am now going through a code cleaning exercise. What I see below is that I now have four event handlers calling the same method. Is it OK to change the event handlers all to point directly to GridMenu, avoiding the extra code? Will this cause me problems later in development?

Obviously at present I am using the default even handler names.

private void grdEnquiriesLevel1_ShowGridMenu(object sender, GridMenuEventArgs e)
    {
        GridMenu(sender, e);
    }

    private void grdApplicantsLevel1_ShowGridMenu(object sender, GridMenuEventArgs e)
    {
        GridMenu(sender, e);
    }

    private void grdApplicationsLevel1_ShowGridMenu(object sender, GridMenuEventArgs e)
    {
        GridMenu(sender, e);
    }

    private void grdInterviewsLevel1_ShowGridMenu(object sender, GridMenuEventArgs e)
    {
        GridMenu(sender, e);
    }

    private void GridMenu(object sender, GridMenuEventArgs e)
    {
        GridView view = (GridView)sender;

        if (view.CalcHitInfo(e.Point).InRow)
            popupMenu1.ShowPopup(Cursor.Position);
    }

解决方案

Instead of registering directly to GridMenu, create a general event handler named Grid_ShowGridMenu.

Just register to the same event handler for each grid, instead of creating a separate event handler per grid.

grdEnquiriesLevel1.ShowGridMenu += Grid_ShowGridMenu;
grdApplicantsLevel1.ShowGridMenu += Grid_ShowGridMenu;
grdApplicationsLevel1.ShowGridMenu += Grid_ShowGridMenu;
grdInterviewsLevel1.ShowGridMenu += Grid_ShowGridMenu;


private void Grid_ShowGridMenu(object sender, GridMenuEventArgs e)
{
    GridMenu((GridView)sender, e.Point); 
}

Now, instead of passing sender, e directly to GridMenu, pass only necessary values to GridMenu and change the signature of GridMenu so it can be more reusable.

private void GridMenu(GridView grid, Point hitPoint) 
{
    if (grid.CalcHitInfo(hitPoint).InRow)
        popupMenu1.ShowPopup(Cursor.Position);
}

这篇关于在C#中重用事件处理好的做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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