Imajin Shaw

Imajin Shaw

  • NA
  • 3
  • 0

Stopping child event in parent?

Feb 12 2008 5:40 AM
Hi, I was wondering if you guys can help me sort this out. I've basically got a custom button which inherits from the Button class, and this button is used throughout my forms. The custom button has a state 'permission' and what basically happens is that if this button is clicked on any form, it checks if the button has an associated 'permission', and the event action in the child form must only be fired if the person clicking the button has that 'permission'. My code currently looks something like this (simplified, of course):
  1. public class MyButton : Button
  2. {
  3.   private string permission;
  4.  
  5.   public MyButton() : base
  6.   {
  7.     this.Click += new EventHandler(MyButton_Click);
  8.   }
  9.  
  10.   public string Permission
  11.   {
  12.     set
  13.     {
  14.       this.permission = value;
  15.     }
  16.   }
  17.  
  18.   private bool PersonHasPermission(permission)
  19.   {
  20.     //checks if the person has the permission
  21.   }
  22.  
  23.   private void MyButton_Click(object sender, EventArgs e)
  24.   {
  25.     if (permission != null && permission != "")
  26.     {
  27.       if (!PersonHasPermission(permission))
  28.       {
  29.         //what do i put here to stop the event in MyForm from firing further?
  30.       }
  31.     }
  32.   }
  33.  
  34. }
  35.  
  36. public class MyForm
  37. {
  38.   public MyForm()
  39.   {
  40.     MyButton myButton = new MyButton();
  41.     myButton.Click += new EventHandler(myButton_Click);
  42.     myButton.Permission = "Can_click_this_button";
  43.   }
  44.  
  45.   private void myButton_Click(object sender, EventArgs e)
  46.   {
  47.     //this is the stuff that I want to happen only if the person has the required permission, else do nothing, but i do not want to do an explicit check here, since it is already being done in the MyButton class
  48.   }
  49.  
  50. }
If you look at line 29 in my code, I need something to put there to stop the event from firing in the child button, which is on line 47, but I do not want to put an explicit check there, as that is already done in the parent class. Another reason is that making the check explicit would entail a LOT of changes having to be made because the button is already used on a lot of forms, so I really want to minimize the code changes. Someone please help me :(

Answers (3)