473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C#-APP: Stopping child event in parent?

3
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):
Expand|Select|Wrap|Line Numbers
  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. }
Someone please help? Thank you
Feb 6 '08 #1
4 1380
imajin
3
Please help me! Or tell me if you you have some other approach I can look at :(
Feb 7 '08 #2
Plater
7,872 Expert 4TB
You want this button to only work if a certain condition is met? And you want to be able to use the button anywhere?

In your inhereited button class, if you add STATIC member variables, they will be the SAME for every instance of your button.
So if you add your permission stuff as statics to your custom class, when your special button is clicked (regardless of which form it is on) it can check against those "permissions" to see if it should perform an action or not.
Feb 7 '08 #3
imajin
3
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 :(
Feb 12 '08 #4
Plater
7,872 Expert 4TB
Is the action the button performs different for each use? Actually nevermind, I think I have a possible solution.

In your derrived button class, you need to do some sneainess.
I *think* you can do this:
Inside the derrived class, add a listener to the button click.
Inside the derrived class, create a custom event with the same name as the click event. (You would need to use the override, if you cannot, I guess you need to name it like SecureClick or something)
Then, in th event handler you made for the click event inside your derrived class, you would perform the security check stuff you wanted, and if it passes, trigger your custom event to fire.

My hope is that you can override the Click event in the derrived class to perform the permissions check, then either fire the event or cancel it, so the form never even gets the button click event.
If you can't override it, make that SecureClick event and have the forms listen for that instead of the Click event.
Feb 12 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: David Sobey | last post by:
hi Here's some code: void SomeFunc(childClass arg); .... parentClass a; a=new childClass(); SomeFunc(a);
1
by: Aaron Smith | last post by:
I have a parent table that has one child table. The parent has a single field (ID, AutoIncrement, Seed 0, Step -1 in the DataSet, Seed 1, step 1, in DataSource). The child is linked to this ID...
3
by: luna | last post by:
i have a parent form which opens a child form, i need to pass a variable back to parent then reload parent so parentopens child, child passes to parent, child closes, parent reloads its only...
7
by: aharding | last post by:
Hello! I have a form that opens with the click of a toggle button on another form. I would like to link the child and parent fields of the forms for data input but am not experienced enough with...
6
by: blaine | last post by:
Hello, I'm currently overriding function keys (F1 to F4) to perform other actions. In order to do this the default popup windows of Help (F1), Seach(F3) etc must be turned off. In FF it's easy...
2
by: ChaoticDALO | last post by:
Hello, I am wondering if this is possible. If I have a parent class and a child that inherits the parent, the child can access a parents member function right? Well let's say we are in the parent...
0
by: Sep410 | last post by:
How can I hide some of the child columns in child and parent data grid?
8
by: Sep410 | last post by:
I am looking for a way to have child and parent datagrid in my form. Can anyone help me?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.