- public class MyButton : Button
- {
- private string permission;
- public MyButton() : base
- {
- this.Click += new EventHandler(MyButton_Click);
- }
- public string Permission
- {
- set
- {
- this.permission = value;
- }
- }
- private bool PersonHasPermission(permission)
- {
- //checks if the person has the permission
- }
- private void MyButton_Click(object sender, EventArgs e)
- {
- if (permission != null && permission != "")
- {
- if (!PersonHasPermission(permission))
- {
- //what do i put here to stop the event in MyForm from firing further?
- }
- }
- }
- }
- public class MyForm
- {
- public MyForm()
- {
- MyButton myButton = new MyButton();
- myButton.Click += new EventHandler(myButton_Click);
- myButton.Permission = "Can_click_this_button";
- }
- private void myButton_Click(object sender, EventArgs e)
- {
- //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
- }
- }
Stopping child event in parent?
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):
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 :(
AlanPosted Feb 12, 2008, 8:31 AM
I can't see any alternative to unwiring the eventhandler in the forms but you could do this in the constructor (after the call to InitializeComponent) if you're not using form_Load.
However, provided the eventhandler has exactly the same name in all of your forms, the call to the form's eventhandler could be made from the custom Button class using reflection without the need to hardcode the class names. In fact you wouldn't even need to change the accessibility because you can call private methods using reflection.
Imajin ShawPosted Feb 12, 2008, 7:11 AM
The button won't know what the underlying class(es) will be because there's too many of them :(
So using that solution, I'd have to make two changes:
1) Unwire the eventhandler on every form;
2) Make provision for every possible form that uses the custom MyButton in the MyButton eventhandler;
neither of which is an option, as there are simply too many forms and too little time :(
AlanPosted Feb 12, 2008, 6:27 AM
Here's an approach which might work in that scenario:
1. Don't wire up MyForm's myButton click eventhandler at compile time or, if it's already been wired up by the designer, unwire it in MyForm_Load:
myButton.Click -= new EventHandler(myButton_Click);
However, change its accessibility from private to internal.
2. Now, in MyButton's MyButton_Click eventhandler, if the person has the required permission call MyForm's eventhandler directly - otherwise do nothing:
private void MyButton_Click(object sender, EventArgs e)
{
if (permission != null && permission != "")
{
if (PersonHasPermission(permission))
{
MyForm myForm = (MyForm)this.FindForm();
myForm.myButton_Click(sender, e);
}
}
}
Needless to say, this approach - if it works at all - will only work if the custom Button knows the class name of its containing form as this needs to be hard-coded to be able to call the method.