code_junkie

Is there a type of object, to which I can cast both Buttons AND MenuItem objects in order to access their Tag properties?

Nov 12th, 2011
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. void itemClick(object sender, EventArgs e)
  2. {
  3. Control c = (Control)sender;
  4. MethodInvoker method = new MethodInvoker(c.Tag.ToString(), "Execute");
  5. method.Invoke();
  6. }
  7.  
  8. object tag;
  9. Button button;
  10. MenuItem menuItem = sender as MenuItem;
  11. if (menuItem != null)
  12. {
  13. tag = menuItem.Tag;
  14. }
  15. else if( (button = sender as Button) != null )
  16. {
  17. tag = button.Tag;
  18. }
  19. else
  20. {
  21. //not button nor MenuItem
  22. }
  23.  
  24. object myControlOrMenu = sender as MenuItem ?? sender as Button;
  25. if (myControlOrMenu == null)
  26. // neither of button or menuitem
Add Comment
Please, Sign In to add comment