如何为Windows窗体或UserControls继承的用户访问创建通用类 [英] How do I create a Generic Class for User Access that can be inherited by Windows Forms or UserControls

查看:64
本文介绍了如何为Windows窗体或UserControls继承的用户访问创建通用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的表单和用户控件上的控件实现一个简单的用户访问权限方案。基本上这就是我要找的,

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,

If (UserCanModify) { TextBox1.Readonly = false;}



我需要通过表单或用户控件来完成 - 所以我想如果我创建了一个继承的类,我可以自动地以某种方式设置这些权限。

基本上这就是我的想法:

MyForm:Form,MyAccessClass



MyControl:UserControl, MyAccessClass



其中MyAccessClass可以设置对Forms / UserControls的访问权限 - 控件只读,启用按钮等。



我承认这是我第一次在应用程序上实现任何类型的安全性而且很简单,用户除了查看之外不应该做什么,主管应该能够改变这个或者......



这是一个很好的方法,简单就是更好,自动化当然更好。我知道如果我有一个类,我的表单可以继承它会很好 - 但我不知道怎么做...任何例子都欢迎和赞赏,谢谢。


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.

推荐答案

你不能继承一个Form和一个UserControl的类:它们都是从Control派生出来的,但它们都是以不同的方向分支。由于你只能从一个基类(Form或UserControl)继承,你不能创建一个公共类来继承Forms和UserControls。



最好的你可以do是创建一个接口,可以将其添加到Form和UserControl的定义中,这可能会强制派生类实现UserCanModify属性和一堆操作它的方法。没有什么可以强制派生类为你实际显示文本框!
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!


这篇关于如何为Windows窗体或UserControls继承的用户访问创建通用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆