Click here to Skip to main content
15,886,782 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I am trying to implement a simple scheme for user access rights to controls on my forms and user controls. Basically this is what I am looking for,
C#
If (UserCanModify) { TextBox1.Readonly = false;}

I need to do it through out the form or user control - so I thought if I created an inherited class that I could set those rights either automagically somehow.
Basically this is what I am thinking :
MyForm : Form, MyAccessClass
Or
MyControl : UserControl, MyAccessClass

Where MyAccessClass would do the magic of setting access to the Forms/UserControls - controls for read only, button enabled etc..

I admit this is the first time I am implementing any kind of security on an application and it is just simple a user should not be able to do nothing but view and a supervisor should be able to change this or that..

What is a good approach to this , simple is better , automagic of course even better. I know if I have a class that my forms could inherit it would be nice - but again I am not sure how to do this.. any examples are welcome and appreciated, thank you.
Posted
Updated 12-Jun-15 8:31am
v2
Comments
virusstorm 12-Jun-15 12:24pm    
I would suggest looking at implementing an MVVM pattern. Take a look at this article, it might help give you a starting point.

http://www.codeproject.com/Articles/364485/MVVM-Model-View-ViewModel-Patte

1 solution

You can't inherit a class to both a Form and a UserControl: they are both derived from Control but they each "branch" in a different direction. And since you can only inherit from one base class (Form or UserControl) you can't create a common class to inherit Forms and UserControls from.

The best you can do is create an Interface which can be added to the definition of both your Form and UserControl, and that could force both derived classes to implement a UserCanModify property and a bunch of methods to manipulate it. There is nothing that can force either derived class to actually display the textbox for you though!
 
Share this answer
 
Comments
stixoffire 12-Jun-15 14:28pm    
I understand the first part , what I was meaning is to make a class and have MyForm : Form, MyAccessClass Or MyControl : UserControl, MyAccessClass - My AccessClass providing the methods for the form , somehow setting the controls on either parent such as DataGridView, TextBox to an accessrequired and binding the property to this - is there an easy way to do it ? I know on Textbox I can bind to read only property: I gues what I am after is something a bit more elegant and simplistic so I simply let the ApplicationUser (if that changes from another form - then any open form will update its controls appropriately sensing the user has changed. My UserAccessClass is what would do the work so to speak - just how to do that eludes me.
OriginalGriff 12-Jun-15 14:36pm    
You can't do that at all in C#: a class can only derive from a single base class (in your case Form or UserControl)
So you can't say
public class MyClass : Form, MyAccessClass
at all. all you can do is
public class MyClass : Form, IMyAccessInterface
And an interface cannot contain any concrete properties, methods, field, or events - it's just a "contract" that classes that implement the interface will implement the required properties etc. or they will not compile.
Sorry - but you can't do it!

If it's any consolation, a couple of times I've come up with a brilliant design that required Abstract Static Methods and got half way through implementation before realising you can't have them either... :sigh:
I was annoyed the first time, but felt a right idiot the second!
stixoffire 12-Jun-15 17:30pm    
Thank you for the help and the time saver , the harder way and not elegant way to do this is create a base form from which all my forms derive from and a base UserControl (I do not think I like the idea - so I am thinking there must be a different way - I like the simple you know UserChanged -event fires (my Form is subscribed) and then my controls on Form/UserControl - change Accessibility .. one Event , one piece of code to do all the labor. Maybe in the subscription I could pass the forms controls to MyAccessClass and then update them and return them back - I am not sure I like that either but maybe it is what I need to do without repeating code in every form under the sun.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900