4 C#WinForm开发权限管理历程之主控窗体功能实现

0 C#WinForm开发权限管理历程之权限管理概述
1 C#WinForm开发权限管理历程之三层架构
2 C#WinForm开发权限管理历程之新建项目
3 C#WinForm开发权限管理历程之Form1完善

4.1 MDIForm的Load

先用代码给treeview1加几个节点

 private void MDIForm_Load(object sender, EventArgs e)
        {
            //创建treeview根节点
            TreeNode tn0 = treeView1.Nodes.Add("我的导航");

            TreeNode tn1 = new TreeNode("基础数据");
            tn1.Tag = "1000";
            TreeNode tn2 = new TreeNode("业务数据");
            tn2.Tag = "2000";
            TreeNode tn3 = new TreeNode("系统管理");
            tn3.Tag = "9000";

            tn0.Nodes.Add(tn1);
            tn0.Nodes.Add(tn2);
            tn0.Nodes.Add(tn3);

            treeView1.ExpandAll();
        }

4.2 看一下运行效果

我的导航运行效果

4.3 创建3个Winform

在AppUI项目下创建1个文件夹SDI,用来存放Winform
创建3个WinForm分别为jic.cs 、yew.cs 、xit.cs 每个窗体上放一个Label用来标识是不同的窗体。
完成后大概就是这样:

3个Form

注意这里的3个窗体的命名空间为AppUI.SDI

4.4用窗体名称在Panel2里打开窗体Form

-打开MDIForm的代码,引入2个命名空间

using System.Reflection;
using AppUI.SDI;

-创建一个打开窗体的方法

 private void ShowForm(Form sdi) //在panel里打开窗体
        {
            this.splitContainer1.Panel2.Controls.Clear();
            sdi.TopLevel = false;
            //sdi.FormBorderStyle = FormBorderStyle.FixedDialog;
            //sdi.FormBorderStyle = FormBorderStyle.None;
            sdi.FormBorderStyle = FormBorderStyle.SizableToolWindow;
            sdi.Dock = DockStyle.Fill;
            sdi.Parent = this.splitContainer1.Panel2;
            this.splitContainer1.Panel2.Controls.Add(sdi);
            sdi.Show();
        }

-treeView1的NodeMouseDoubleClick事件代码

private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            string formName = ""; //要打开的窗体:命名空间.类名
            TreeNode tn = e.Node;

            string tag = (string)tn.Tag;
            if (string.IsNullOrEmpty(tag)) return;
            if (tag == "1000") formName = "JIC";
            else if (tag == "2000") formName = "YEW";
            else if (tag == "9000") formName = "XIT";
            else return;

            Assembly assembly = Assembly.GetExecutingAssembly();
            // 实例化窗体
            formName = "AppUI.SDI." + formName;
            Form form = assembly.CreateInstance(formName) as Form;

            ShowForm(form);
        }

4.5运行效果如下:

主控功能效果

4.6主控窗体的完整代码:

using System;
using System.Windows.Forms;
using System.Reflection;
using AppUI.SDI;

namespace AppUI
{
    public partial class MDIForm : Form
    {
        public MDIForm()
        {
            InitializeComponent();
        }

        private void MDIForm_Load(object sender, EventArgs e)
        {
            //创建treeview根节点
            TreeNode tn0 = treeView1.Nodes.Add("我的导航");

            TreeNode tn1 = new TreeNode("基础数据");
            tn1.Tag = "1000";
            TreeNode tn2 = new TreeNode("业务数据");
            tn2.Tag = "2000";
            TreeNode tn3 = new TreeNode("系统管理");
            tn3.Tag = "9000";

            tn0.Nodes.Add(tn1);
            tn0.Nodes.Add(tn2);
            tn0.Nodes.Add(tn3);

            treeView1.ExpandAll();
        }

        
        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            string formName = ""; //要打开的窗体:命名空间.类名
            TreeNode tn = e.Node;

            string tag = (string)tn.Tag;
            if (string.IsNullOrEmpty(tag)) return;
            if (tag == "1000") formName = "JIC";
            else if (tag == "2000") formName = "YEW";
            else if (tag == "9000") formName = "XIT";
            else return;

            Assembly assembly = Assembly.GetExecutingAssembly();
            // 实例化窗体
            formName = "AppUI.SDI." + formName;
            Form form = assembly.CreateInstance(formName) as Form;

            ShowForm(form);
        }

        private void ShowForm(Form sdi) //在panel里打开窗体
        {
            this.splitContainer1.Panel2.Controls.Clear();
            sdi.TopLevel = false;
            //sdi.FormBorderStyle = FormBorderStyle.FixedDialog;
            //sdi.FormBorderStyle = FormBorderStyle.None;
            sdi.FormBorderStyle = FormBorderStyle.SizableToolWindow;
            sdi.Dock = DockStyle.Fill;
            sdi.Parent = this.splitContainer1.Panel2;
            this.splitContainer1.Panel2.Controls.Add(sdi);
            sdi.BringToFront();
            sdi.Show();
        }
    }
}

5 C#WinForm开发权限管理历程之功能管理

你可能感兴趣的:(4 C#WinForm开发权限管理历程之主控窗体功能实现)