tags:

views:

440

answers:

3

In my current project I am using 4 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 I now have 4 event handlers calling the same method, is it ok to change the event handlers all to point direct 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);
    }
A: 

As long as the event code is generic for all controls then this method is fine and clean

If you start having major if/else or switch blocks in the code then maybe time for a re-think

TFD
How can repeating yourself 5 times be called "clean"? There's no reason to have more than one event handling method here, which either does the work itself or defers to another method with a different signature.
Jon Skeet
Read in the +ve. "...then this method is fine and clean.." refers to @petebob796 saying "is it ok to change the event handlers all to point direct to GridMenu...". You shouldn't assume the -ve in a posting just because the langauge isn't perfect!
TFD
+9  A: 

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);
}
Sung Meister
@Rich B: Thanks Rich B, I just found out about those backticks after reading through your edits.
Sung Meister
A: 

You should make a User Control that encapsulates your custom Grid. That way, all of your behavior will be encapsulated and reusable.

Andrew Peters
What if it doesn' tneed to be reused?
TFD
In this case it is being used 4 times - hence the code duplication.
Andrew Peters