公共类:

static class PowerHelper
    {
        /// <summary>
        /// 设置form上的组件的权限
        /// </summary>
        /// <param name="form">控件所在的窗体</param>
        /// <param name="getPermissionsHandler">取得form里组件名及其Enabled属性(当前用户)的键值对</param>
        public static void SetPermissions(this Form form, Func<Form, IDictionary<string, bool>> getPermissionsHandler)
        {
            Type t = form.GetType();
            FieldInfo[] fs = t.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
            IDictionary<string, bool> permissions = getPermissionsHandler(form);
            foreach (FieldInfo f in fs)
            {
                dynamic component = f.GetValue(form);
                if (component is System.ComponentModel.Component && permissions.ContainsKey(f.Name))
                {
                    try
                    {
                        //禁用
                        component.Enabled = permissions[f.Name];
                        //隐藏
                        //component.Visible = permissions[f.Name];
                    }
                    catch { }
                }
            }
        }

基类窗体加入验证:

public partial class BaseForm : Form
    {
        public BaseForm()
        {
            InitializeComponent();
        }

private void BaseForm_Load(object sender, EventArgs e)
        {
            Func<Form, IDictionary<string, bool>> powerFunc = ValidatePower;
            PowerHelper.SetPermissions(this, powerFunc);
        }

private Dictionary<string, bool> ValidatePower(Form frm)
        {
            Dictionary<string, bool> powerList = new Dictionary<string, bool>();
            powerList.Add("toolStripButton1", false);
            powerList.Add("button3", false);
            powerList.Add("textBox2", false);
            return powerList;
        }
    }

程序窗体继承基类:

public partial class Form1 : BaseForm
    {
        public Form1()
        {
            InitializeComponent();
        }
    }

Winform窗体控件级权限处理的更多相关文章

  1. winform窗体控件(全)

    回顾跟补充下除了昨天那常用6个其他的winform窗体控件作用 1:Button:按钮 (1)AutoSize:如果是True的情况下,内容将会撑开:False的话会另起一行 (2)Enabled: ...

  2. C# WinForm窗体控件Panel修改边框颜色以及边框宽度方法

    C# WinForm窗体控件Panel修改边框颜色以及边框宽度方法 1.新建组件这里可以自定义一个Panel控件起名为PanelEx 2.增加一个BoderColor属性和BoderSize属性 pr ...

  3. C# WinForm窗体控件GroupBox修改边框颜色控件

    C# WinForm窗体控件GroupBox修改边框颜色控件 1.新建组件这里可以自定义一个GroupBox控件起名为GroupBoxEx 2.增加一个BoderColor属性 private Col ...

  4. 如何在多线程中调用winform窗体控件

    由于 Windows 窗体控件本质上不是线程安全的.因此如果有两个或多个线程适度操作某一控件的状态(set value),则可能会迫使该控件进入一种不一致的状态.还可能出现其他与线程相关的 bug,包 ...

  5. winform窗体 控件【菜单和工具栏控件】【容器控件】

    winform的菜单栏和工具栏    1.ContextMenuStrip   -- 右键菜单     可以绑定在任何一个控件上,添加操作快捷键,并可以设置多层    每行相当于一个按钮,输入-可添加 ...

  6. Winform窗体控件自适应大小

    自己写的winform窗体自适应大小代码,代码比较独立,很适合贴来贴去不会对原有程序造成影响,可以直接继承此类或者把代码复制到自己的代码里面直接使用 借鉴了网上的一些资料,最后采用重写WndProc方 ...

  7. Winform 窗体控件随窗体自动(等比例)调整大小

    新建窗体程序了,添加窗体事件Load(加载窗体时).Resize(调整控件大小时).自定义方法setTag(获取控件的width.height.left.top.字体大小等信息的值).setContr ...

  8. winform窗体 控件【MDI 窗体容器】

    MDI :窗体容器 -- 在窗体中放置窗体 属性 IsMdiContainer  : 是否是窗体          -- 只有 Form 有此属性    Form2 f2 = new Form2(); ...

  9. winform窗体 控件【公共控件】

    Button   按钮 布局 AutoSize    自动匹配尺寸 Location     确定控件位置,相对左上角坐标 Margin       控件与控件之间的距离 Size           ...

随机推荐

  1. LeaderF常用用法

    常用: 搜索当前目录下的文件 :LeaderfFile <leader>f 搜索当前的Buffer :LeaderfBuffer <leader>b 搜索最近使用过的文件( s ...

  2. webserver有哪些

    http://blog.csdn.net/mfsh_1993/article/details/70245380 常用web服务器有Apache.Nginx.Lighttpd.Tomcat.IBM We ...

  3. 3. HashMap和JSONObject用法

    <%@page import="net.sf.json.JSONObject"%><%@page import="java.util.List" ...

  4. angular 中怎么获取路径上的参数 参考:https://docs.angularjs.org/api/ng/service/$location

    参考: https://docs.angularjs.org/api/ng/service/$location

  5. leetcode500

    public class Solution { public string[] FindWords(string[] words) { var list1 = new List<char> ...

  6. 安装vue.js

    1.  下载node https://nodejs.org/dist/v8.11.2/node-v8.11.2-x64.msi 2. 查看npm版本 在cmd下输入命令:npm -v 如果低于3.0版 ...

  7. J2SE 8的输入输出--缓冲

    FileChannel带缓冲 //1. read the point location FileChannel channelRead = FileChannel.open(Paths.get(&qu ...

  8. idea中创建多module的maven工程

    以前自学Java web的时候,我们都是创建一个web工程,该工程下面再创建dao.service.controller等包.自从工作以后,我们会发现现在的web项目包含多个module,contro ...

  9. mysql启动报错 The server quit without updating PID file

    [root@uz6542 data]# /etc/init.d/mysqld startStarting MySQL... ERROR! The server quit without updatin ...

  10. TEXT 3 Food firms and fat-fighters

    TEXT 3 Food firms and fat-fighters 食品公司与减肥斗士 Feb 9th 2006 From The Economist Global Agenda Five lead ...