[WinForm] Simple permission management for WinForm

Source: Internet
Author: User
Tags disk usage

[Abstract] this year, we have successively written fixed assets and low-value consumable products, purchased mold management, and the process management subsystems to be implemented. Each of them has a small program, and each time we build the environment. Package each time you deploy a small system. Simply implement a simple framework.

Permission management is modeled on your own collaborative work platform.

Table 1: menu management

CREATE TABLE [dbo].[WinForm_MenuInfo]([ID] [int] IDENTITY(1,1) NOT NULL,[NAME] [nvarchar](100) NOT NULL,[PARENTID] [int] NOT NULL,[FORMNAME] [nvarchar](200) NULL,[TYPE] [int] NULL,[SORTINDEX] [int] NULL, CONSTRAINT [PK_WinForm_MenuInfo] PRIMARY KEY CLUSTERED ([ID] ASC)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]) ON [PRIMARY]GOALTER TABLE [dbo].[WinForm_MenuInfo] ADD  CONSTRAINT [DF_WinForm_MenuInfo_PARENTID]  DEFAULT ((-1)) FOR [PARENTID]GOALTER TABLE [dbo].[WinForm_MenuInfo] ADD  CONSTRAINT [DF_WinForm_MenuInfo_TYPE]  DEFAULT ((0)) FOR [TYPE]GOALTER TABLE [dbo].[WinForm_MenuInfo] ADD  CONSTRAINT [DF_WinForm_MenuInfo_SORTINDEX]  DEFAULT ((0)) FOR [SORTINDEX]GO

Table 2: role management

CREATE TABLE [dbo].[WinForm_RoleInfo]([ID] [int] IDENTITY(1,1) NOT NULL,[NAME] [nvarchar](100) NOT NULL,[DESCRIPT] [nvarchar](500) NULL,[PARENTID] [int] NOT NULL, CONSTRAINT [PK_WinForm_RoleInfo] PRIMARY KEY CLUSTERED ([ID] ASC)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]) ON [PRIMARY]GO

Table 3: Role menu

CREATE TABLE [dbo].[WinForm_RoleMenu]([id] [int] IDENTITY(1,1) NOT NULL,[rid] [int] NOT NULL,[mid] [int] NOT NULL, CONSTRAINT [PK_WinForm_RoleMenu] PRIMARY KEY CLUSTERED ([id] ASC)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]) ON [PRIMARY]GO

Menu management:


Role authorization:


User role management


Key points:

How to load WINFORM?

Simple layout:

Top title

Tree menu on the left

The right side is TabContorl.

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. data. sqlClient; using System. drawing; using System. linq; using System. text; using System. windows. forms; using ERPSystem. DBUtility; namespace ERPSystem {public partial class FrmMain: Form {public int userid; public FrmMain (int userid) {InitializeComponent (); this. userid = userid;} private void FrmM Ain_Load (object sender, EventArgs e) {this. icon = Icon. fromHandle (new Bitmap (imageList1.Images [2]). getHicon (); // clear the control // this. mainTabControl. tabPages. clear (); // The OwnerDrawFixed method indicates that the drawing size of the form is the same as this. mainTabControl. drawMode = TabDrawMode. ownerDrawFixed; this. mainTabControl. padding = new System. drawing. point (CLOSE_SIZE, CLOSE_SIZE); this. mainTabControl. drawItem + = new DrawItemEventHandler (this. mainT AbControl_DrawItem); this. mainTabControl. mouseDown + = new System. windows. forms. mouseEventHandler (this. mainTabControl_MouseDown); BindTree (); this. trvMeun. expandAll ();} const int CLOSE_SIZE = 10; // tabPage tag image Bitmap image = global: ERPSystem. properties. resources. close; // draw "X", that is, the Close button private void MainTabControl_DrawItem (object sender, DrawItemEventArgs e) {try {Rectangle myTabRect = this. mainTab Control. getTabRect (e. index); // Add the TabPage attribute e. graphics. drawString (this. mainTabControl. tabPages [e. index]. text, this. font, SystemBrushes. controlText, myTabRect. X + 2, myTabRect. Y + 2); // draw another rectangle box using (Pen p = new Pen (Color. white) {myTabRect. offset (myTabRect. width-(CLOSE_SIZE + 3), 2); myTabRect. width = CLOSE_SIZE; myTabRect. height = CLOSE_SIZE; e. graphics. drawRectangle (p, myTabRect);} // fill the rectangle Color recColor = e. State = DrawItemState. Selected? Color. white: Color. white; using (Brush B = new SolidBrush (recColor) {e. graphics. fillRectangle (B, myTabRect);} // draw the close Symbol using (Pen objpen = new Pen (Color. black )) {// ================================================= ============/// draw X /// "\" line // Point p1 = new Point (myTabRect. X + 3, myTabRect. Y + 3); // Point p2 = new Point (myTabRect. X + myTabRect. width-3, myTabRect. Y + myTabRect. height-3); // e. graphics. dr AwLine (objpen, p1, p2); /// "/" // Point p3 = new Point (myTabRect. X + 3, myTabRect. Y + myTabRect. height-3); // Point p4 = new Point (myTabRect. X + myTabRect. width-3, myTabRect. Y + 3); // e. graphics. drawLine (objpen, p3, p4 ); /// =================================================== ============/// use the image Bitmap bt = new Bitmap (image ); point p5 = new Point (myTabRect. x, 4); e. graphics. drawImage (bt, p5); // e. graphics. drawSt Ring (this. mainTabControl. tabPages [e. index]. text, this. font, objpen. brush, p5);} e. graphics. dispose ();} catch (Exception) {}} // disable the button function private void MainTabControl_MouseDown (object sender, MouseEventArgs e) {if (e. button = MouseButtons. left) {int x = e. x, y = e. y; // calculation close area Rectangle myTabRect = this. mainTabControl. getTabRect (this. mainTabControl. selectedIndex); myTabRect. offset (myTabRect. width -(CLOSE_SIZE + 3), 2); myTabRect. width = CLOSE_SIZE; myTabRect. height = CLOSE_SIZE; // If the mouse is in the area, close the tab bool isClose = x> myTabRect. X & x <myTabRect. right & y> myTabRect. Y & y <myTabRect. bottom; if (isClose = true) {this. mainTabControl. tabPages. remove (this. mainTabControl. selectedTab) ;}}/// <summary> // initialize the tree menu /// </summary> private void BindTree () {// customize the menu table, read/* DataTab from XML or database Le tblDatas = new DataTable ("Datas"); tblDatas. columns. add ("ID", Type. getType ("System. int32 "); tblDatas. columns. add ("Title", Type. getType ("System. string "); tblDatas. columns. add ("Name", Type. getType ("System. string "); tblDatas. columns. add ("ParentID", Type. getType ("System. int32 "); tblDatas. rows. add (new object [] {1, "System Management", "", 0}); tblDatas. rows. add (new object [] {2, "electronic process management", "", 0}); tblDatas. Rows. add (new object [] {3, "menu management", "Form1", 1}); tblDatas. rows. add (new object [] {4, "role management", "Form2", 1}); tblDatas. rows. add (new object [] {5, "user authorization", "Form2", 1}); */string strRid = "select WF_RoleId from users where id =" + userid; string rid = string. empty; using (SqlDataReader dr = SqlHelper. executeReader (SqlHelper. conn, CommandType. text, strRid) {if (dr. read () {rid = dr ["WF_RoleId"]. toSt Ring () ;}} string strSql = "select * from dbo. winForm_MenuInfo where ID in (select distinct mid from WinForm_RoleMenu where rid in ("+ rid +") order by sortindex asc "; DataTable tblDatas = SqlHelper. executeDataset (SqlHelper. conn, CommandType. text, strSql ). tables [0]; initParent (tblDatas);} // initialize the root node private void initParent (DataTable dt) {DataRow [] drs = dt. select ("PARENTID =-1"); foreach (DataRo W dr in drs) {TreeNode tn = new TreeNode (); tn. text = dr ["NAME"]. toString (); tn. toolTipText = dr ["FORMNAME"]. toString (); tn. tag = dr ["ID"]. toString (); // tn. imageIndex = 1; this. trvMeun. nodes. add (tn); // initialize the subnode initLeaf (dt, tn) ;}}// initialize the subnode private void initLeaf (DataTable dt, TreeNode tn) {DataRow [] drs = dt. select ("PARENTID =" + tn. tag as string); foreach (DataRow dr in drs) {TreeNode ctn = ne W TreeNode (); ctn. text = dr ["NAME"]. toString (); ctn. toolTipText = dr ["FORMNAME"]. toString (); ctn. tag = dr ["ID"]. toString (); // tn. imageIndex = 1; tn. nodes. add (ctn); // recursive call, continuous loop to leaf node initLeaf (dt, ctn); }}// double-click to close Tab private void MainTabControl_DoubleClick (object sender, EventArgs e) {// Point pt = new Point (e. x, e. y); if (MainTabControl. tabCount> 0) {if (MainTabControl. tabCount> 0) {this. mainTab Control. tabPages. remove (MainTabControl. selectedTab );}}} /// <summary> /// double-click the node to open the form. /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void trvMeun_NodeMouseDoubleClick (object sender, treeNodeMouseClickEventArgs e) {TreeNode node = e. node; if (trvMeun. selectedNode. parent = null) {} else {if (! FindTabControl (node. text. trim () {ERPSystem. moduleClass myfrm = new ModuleClass (); if (myfrm. show_Form (node. text. trim (), node. toolTipText. toString (). trim ())! = Null) {this. mainTabControl. tabPages. add (myfrm. show_Form (node. text. trim (), node. toolTipText. toString (). trim (); this. mainTabControl. selectedIndex = this. mainTabControl. tabPages. count-1;} else {// MessageBox. show ("still under construction ...! "," ERROR ");}}}} /// <summary> // check whether a tab with the same name exists in the tab set. // </summary> private bool FindTabControl (string tabName) {bool flag = false; foreach (TabPage item in MainTabControl. tabPages) {if (item. text = tabName) {flag = true ;}} return flag ;}}}

ModuleClass. cs

Using System; using System. collections. generic; using System. linq; using System. text; using System. windows. forms; using System. reflection; using System. runtime. remoting; namespace ERPSystem {class ModuleClass {public TabPage Show_Form (string FormTitle, string FormName) {string strNameSpace = "ERPSystem"; if (FormName! = "") {ObjectHandle obj = Activator. createInstance (null, strNameSpace + ". "+ FormName); // obj. unwrap returns the encapsulated object Form frm = (Form) obj. unwrap (); return initFrom (frm, FormTitle);} else {return null;}/* switch (FormTitle) {case "menu management": string formname = "Form1 "; case "database disk usage": ERPSystem. form1 frm2 = new ERPSystem. form1 (); return initFrom (frm2, FormTitle); case "IO": ERPSystem. form1 frm3 = new ERPSystem. form1 (); return initFrom (frm3, FormTitle); case "job status": ERPSystem. form1 frm4 = new ERPSystem. form1 (); return initFrom (frm4, FormTitle); case "memory usage": ERPSystem. form1 frm5 = new ERPSystem. form1 (); return initFrom (frm5, FormTitle); default: return null ;} * //} /// <summary> /// the form is dynamically loaded to the tab /// </summary> /// <param name = "frm"> </param> /// <param name = "Name"> </param> /// <returns> </returns> Private TabPage initFrom (Form frm, String Name) {if (! String. isNullOrEmpty (Name) {frm. text = Name; frm. topLevel = false; TabPage tp = new TabPage (Name); frm. formBorderStyle = FormBorderStyle. none; tp. controls. add (frm); frm. dock = DockStyle. fill; frm. show (); return tp;} else {return null ;}}}}

Test:







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.