c# - Where to define the menu items event handler -


i have datagridview on winform. when user right-click on grid, showing popup menu. there 3 kinds of popup menu according right-click has been done:

  1. on row defined readonly
  2. on row can edited
  3. on other place in grid (rowindex -1)

i created new classes manage popup menus generation.

public class gridpopupmenufactory {     public contextmenu getmenu(int rowindex, bool isreadonlyrow)     {         basemenu _menu = null;          switch (rowindex)         {             case -1:                 _menu = new generalmenu();                 break;             default:                 if (isreadonlyrow)                 {                     _menu = new readonlyrowmenu();                 }                 else                 {                     _menu = new editablerowmenu();                 }                 break;         }          return _menu.menu;     } }  public abstract class basemenu {     protected contextmenu _menu;     public contextmenu menu     {                 {             if (_menu.menuitems.count == 0)             {                 generatemenu();             }             return _menu;         }     }      protected abstract void generatemenu(); }  public class generalmenu : basemenu {     protected override void generatemenu()     {         var contextmenu = new contextmenu();          contextmenu.menuitems.add(new menuitem("sort"));         contextmenu.menuitems.add(new menuitem("print"));                      _menu = contextmenu;     } }  public class readonlyrowmenu : basemenu {     protected override void generatemenu()     {         var contextmenu = new contextmenu();          contextmenu.menuitems.add(new menuitem("view"));                      _menu = contextmenu;     } }  public class editablerowmenu : basemenu {     protected override void generatemenu()     {         var contextmenu = new contextmenu();          contextmenu.menuitems.add(new menuitem("add"));         contextmenu.menuitems.add(new menuitem("edit"));         contextmenu.menuitems.add(new menuitem("delete"));          _menu = contextmenu;     } }  // winform private void shipmentdetailsdatagridview_mouseclick(object sender, mouseeventargs e)     {         if (e.button == mousebuttons.right)         {             var rowindex = shipmentdetailsdatagridview.hittest(e.x, e.y).rowindex;             var popupmenu = gridpopupmenu.getmenu(rowindex, false);  // 2nd parameter can true, todo              popupmenu.show(shipmentdetailsdatagridview, new point(e.x, e.y));         }     } 

my question correct place define event handler each 1 of menu items?

update: after clicking on menu item, whole operation done on datagridview itself. example, clicking on add, open temp form required field , information added new row in datagridview.

i believe should looks this:

public class generalmenu : basemenu {     protected override void generatemenu() {         var contextmenu = new contextmenu();         contextmenu.menuitems.add(new menuitem("sort", onsort));         contextmenu.menuitems.add(new menuitem("print", onprint));         _menu = contextmenu;     }     void onsort(object sender, eventargs e) {         var gridview = getsourcecontrol<datagridview>(sender);         if(gridview != null) {             // sort         }     }     void onprint(object sender, eventargs e) {         var gridview = getsourcecontrol<datagridview>(sender);         if(gridview != null) {             // print         }     }     static tcontrol getsourcecontrol<tcontrol>(object sender) tcontrol : control {         var menu = ((menuitem)sender).getcontextmenu();         return (menu != null) ? menu.sourcecontrol tcontrol : null;     } } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -