⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmpermit.cs

📁 车载GPS?今天这个就是最好的工具
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace GEROSYS
{
	/// <summary>
	/// frmPermit 的摘要说明。
	/// </summary>
	public class frmPermit : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TreeView treeViewRunc;
		private System.Windows.Forms.ImageList imageListFunc;
		private System.ComponentModel.IContainer components;

        private ArrayList funcName=new ArrayList();
		private System.Windows.Forms.CheckedListBox lstRole;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button1;
		static bool Flag;
		private string lsRoleId;
		TreeNode Root=new TreeNode();
		public frmPermit()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.lstRole = new System.Windows.Forms.CheckedListBox();
			this.treeViewRunc = new System.Windows.Forms.TreeView();
			this.imageListFunc = new System.Windows.Forms.ImageList(this.components);
			this.panel2 = new System.Windows.Forms.Panel();
			this.label2 = new System.Windows.Forms.Label();
			this.panel1 = new System.Windows.Forms.Panel();
			this.label1 = new System.Windows.Forms.Label();
			this.button2 = new System.Windows.Forms.Button();
			this.button1 = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.panel2.SuspendLayout();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.lstRole);
			this.groupBox1.Controls.Add(this.treeViewRunc);
			this.groupBox1.Controls.Add(this.panel2);
			this.groupBox1.Controls.Add(this.panel1);
			this.groupBox1.Location = new System.Drawing.Point(32, 40);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(624, 400);
			this.groupBox1.TabIndex = 9;
			this.groupBox1.TabStop = false;
			// 
			// lstRole
			// 
			this.lstRole.Location = new System.Drawing.Point(30, 56);
			this.lstRole.MultiColumn = true;
			this.lstRole.Name = "lstRole";
			this.lstRole.Size = new System.Drawing.Size(232, 276);
			this.lstRole.TabIndex = 9;
			this.lstRole.ThreeDCheckBoxes = true;
			this.lstRole.SelectedValueChanged += new System.EventHandler(this.lstRole_SelectedValueChanged);
			this.lstRole.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.lstRole_ItemCheck);
			// 
			// treeViewRunc
			// 
			this.treeViewRunc.CheckBoxes = true;
			this.treeViewRunc.ImageList = this.imageListFunc;
			this.treeViewRunc.Location = new System.Drawing.Point(312, 56);
			this.treeViewRunc.Name = "treeViewRunc";
			this.treeViewRunc.Size = new System.Drawing.Size(288, 280);
			this.treeViewRunc.TabIndex = 2;
			this.treeViewRunc.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeViewRunc_AfterSelect);
			// 
			// imageListFunc
			// 
			this.imageListFunc.ImageSize = new System.Drawing.Size(16, 16);
			this.imageListFunc.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// panel2
			// 
			this.panel2.Controls.Add(this.label2);
			this.panel2.Location = new System.Drawing.Point(312, 17);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(288, 32);
			this.panel2.TabIndex = 1;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(72, 8);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(136, 16);
			this.label2.TabIndex = 0;
			this.label2.Text = "功能";
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.label1);
			this.panel1.Location = new System.Drawing.Point(32, 16);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(232, 32);
			this.panel1.TabIndex = 0;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(80, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(128, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "角色";
			// 
			// button2
			// 
			this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.button2.Location = new System.Drawing.Point(399, 470);
			this.button2.Name = "button2";
			this.button2.TabIndex = 13;
			this.button2.Text = "取消";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(223, 470);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(64, 24);
			this.button1.TabIndex = 12;
			this.button1.Text = "确认";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// frmPermit
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(696, 528);
			this.ControlBox = false;
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmPermit";
			this.Resize += new System.EventHandler(this.frmPermit_Resize);
			this.Load += new System.EventHandler(this.frmPermit_Load);
			this.groupBox1.ResumeLayout(false);
			this.panel2.ResumeLayout(false);
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void lstRole_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
		{

		}
		private TreeNode IniReadTree(TreeNode CurrentNode)
		{
			try 
			{
				if (CurrentNode.Tag =="root" && Flag) 
					return CurrentNode;
				for (int liCount=0;liCount<funcName.Count;liCount++)
				{
					if (CurrentNode.Text.ToString()==funcName[liCount].ToString())
					{
						if (CurrentNode.Checked ==false)
							CurrentNode.Checked =true;
						break;
					}
				}
				if (CurrentNode.Nodes.Count> 0 && CurrentNode.Tag !="Over")
				{
					if (CurrentNode.Tag !="root") 
						CurrentNode.Tag ="Over";
					CurrentNode = IniReadTree(CurrentNode.FirstNode);
				}
				else if( CurrentNode.Parent!=null && CurrentNode == CurrentNode.Parent.LastNode)
				{
					Flag=true;
					CurrentNode =IniReadTree(CurrentNode.Parent);
				}
				else
				{
					CurrentNode = IniReadTree(CurrentNode.NextNode);
				}
			} 
			catch(Exception ms)
			{
				MessageBox.Show(ms.Message, "General Error");
			}
			return CurrentNode;
		}
		private void frmPermit_Load(object sender, System.EventArgs e)
		{
			
			this.Location=new Point(176,66);
			AccessData AppClient=new AccessData ();
			DataTable dtRole =new DataTable();
			int llCount;
			string strSQL;
			try 
			{
				strSQL = "SELECT role_id,role_name  FROM role ";
				dtRole=AppClient.SelectSQL(strSQL);
				
				for (llCount=0;llCount<dtRole.Rows.Count;llCount++)
				{
					DataRow drRole;
					drRole=dtRole.Rows[llCount];
					lstRole.Items.Add(drRole["role_name"]);
				}
				Permit p=new Permit();
				p.TreeViewLoadNote(this.treeViewRunc);
			} 
			catch(Exception ms)
			{
				MessageBox.Show(ms.Message, "数据库连接错误");
			}
            
		}

		private void frmPermit_Resize(object sender, System.EventArgs e)
		{
			
			this.Location=new Point(176,66);
			
		}

		private void lstRole_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			this.button1.Enabled =false;
			AccessData AppClient=new AccessData ();
			DataTable dtFunc =new DataTable();
			try 
			{
				lsRoleId=getIdByRoleName();
				if (lsRoleId==null||lsRoleId=="") 
				{
					MessageBox.Show("角色没有选定!");
					return;
				}
				string strSQL="delete from permit where role_id='" +lsRoleId+"'";
				AppClient.ExecuteSQL(strSQL);

				foreach( TreeNode RoleTreeNode in this.treeViewRunc.Nodes)
				{
					if (RoleTreeNode.Checked)
					{
						RoleTreeNode.Tag ="root";
						Flag=false;
						Root=RecursionTree(RoleTreeNode);
					}
				}
			} 
			catch(Exception ms)
			{
				MessageBox.Show(ms.Message, "General Error");
			}
		}
		private string getIdByRoleName()   
		{
			AccessData AppClient=new AccessData ();
			DataTable dtFunc =new DataTable();
			string lsId="";
			try 
			{
				if (this.lstRole.CheckedItems.Count >1) 
				{
					MessageBox.Show("选择的角色太多,请重新选定");
					return null;
				}
				string strSQL = "SELECT  * FROM role where role_name='" + this.lstRole.CheckedItems[0]+"'";
				dtFunc=AppClient.SelectSQL(strSQL);
				DataRow drFunc;
				drFunc=dtFunc.Rows[0];
				lsId= drFunc["role_id"].ToString();
				dtFunc.Dispose();
			} 
			catch(Exception ms)
			{
				MessageBox.Show(ms.Message, "General Error");
			}
			return lsId;
		}
		private string getIdByFuncName(string lsName)
		{
			AccessData AppClient=new AccessData ();
			DataTable dtFunc =new DataTable();
			string lsId="";
			try 
			{
				string strSQL = "SELECT  *  FROM func where func_caption='" + lsName+"'";
				dtFunc=AppClient.SelectSQL(strSQL);
				DataRow drFunc;
				drFunc=dtFunc.Rows[0];
				lsId= drFunc["func_id"].ToString();
				dtFunc.Dispose();
			} 
			catch(Exception ms)
			{
				MessageBox.Show(ms.Message, "General Error");
			}
			return lsId;
		}

		public TreeNode RecursionTree(TreeNode CurrentNode) 
		{
			
			try 
			{

				if (CurrentNode.Tag =="root" && Flag) 
					return CurrentNode;
				if (CurrentNode.Checked)
					Submit(CurrentNode.Text);
				if (CurrentNode.Nodes.Count> 0 && CurrentNode.Tag !="OverIns")
				{
					if (CurrentNode.Tag !="root") 
						CurrentNode.Tag ="OverIns";
					CurrentNode = RecursionTree(CurrentNode.FirstNode);
				}
				else if( CurrentNode.Parent!=null && CurrentNode == CurrentNode.Parent.LastNode)
				{
					Flag=true;
					CurrentNode =RecursionTree(CurrentNode.Parent);
				}
				else
				{
					CurrentNode = RecursionTree(CurrentNode.NextNode);
				}
			} 
			catch(Exception ms)
			{
				MessageBox.Show(ms.Message, "General Error");
			}
			return CurrentNode;
	    }

		private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
		{
			foreach(TreeNode node in treeNode.Nodes)
			{
				node.Checked = nodeChecked;
				if(node.Nodes.Count > 0)
				{
					this.CheckAllChildNodes(node, nodeChecked);
				}
			}
		}

		private void Submit(string lsFuncName)
        { 
			string lsFuncId=getIdByFuncName(lsFuncName);
			AccessData AppClient=new AccessData ();
			DataTable dtFunc =new DataTable();
			string strSQL="select * from permit where role_id='" + lsRoleId +"' and func_id='" + lsFuncId+"'";
            dtFunc=AppClient.SelectSQL(strSQL);
		    if (dtFunc.Rows.Count<1 )
			{
				strSQL ="INSERT permit(role_id,func_id) VALUES('" +
					lsRoleId+"','" +
					lsFuncId + "')" ;
				AppClient.ExecuteSQL(strSQL);
			}
			dtFunc.Dispose();
        }

		private void lstRole_SelectedValueChanged(object sender, System.EventArgs e)
		{
			if (this.lstRole.CheckedItems.Count <1)
			{
				CancelRole();
				return;
			}
			AccessData AppClient=new AccessData ();
			DataTable dtRole =new DataTable();
			string strSQL;
			try 
			{
				string lsRoleId=getIdByRoleName();
				if (lsRoleId==""||lsRoleId==null) 
				{
					MessageBox.Show("角色没有选定!");
					return;
				}
				strSQL = "SELECT a.role_id,b.func_caption  FROM permit a,func b where a.func_id=b.func_id and a.role_id='"+lsRoleId+"'";
				dtRole=AppClient.SelectSQL(strSQL);
				if (dtRole.Rows.Count==0)
					return;
				funcName.Clear();

				for (int llCount=0;llCount<dtRole.Rows.Count;llCount++)
				{
					DataRow drRole;
					drRole=dtRole.Rows[llCount];
					funcName.Add(drRole["func_caption"]);
				}

				foreach( TreeNode RoleTreeNode in this.treeViewRunc.Nodes)
				{
					if (true)
					{
						RoleTreeNode.Tag ="root";
						Flag=false;
						Root=IniReadTree(RoleTreeNode);
					}
				}
				
			} 
			catch(Exception ms)
			{
				MessageBox.Show(ms.Message, "数据库连接错误");
			}
		}
		private void CancelRole()
		{
			foreach( TreeNode RoleTreeNode in this.treeViewRunc.Nodes)
			{
				if (true)
				{
					RoleTreeNode.Tag ="root";
					Flag=false;
					Root=CancelReadTree(RoleTreeNode);
				}
			}
		}
		private TreeNode CancelReadTree(TreeNode CurrentNode)
		{
			try 
			{
				if (CurrentNode.Tag =="root" && Flag) 
					return CurrentNode;
				if (CurrentNode.Checked ==true)
					CurrentNode.Checked =false;
				if (CurrentNode.Nodes.Count> 0 && CurrentNode.Tag !="OverCancel")
				{
					if (CurrentNode.Tag !="root") 
						CurrentNode.Tag ="OverCancel";
					CurrentNode = CancelReadTree(CurrentNode.FirstNode);
				}
				else if( CurrentNode.Parent!=null && CurrentNode == CurrentNode.Parent.LastNode)
				{
					Flag=true;
					CurrentNode =CancelReadTree(CurrentNode.Parent);
				}
				else
				{
					CurrentNode = CancelReadTree(CurrentNode.NextNode);
				}
			} 
			catch(Exception ms)
			{
				MessageBox.Show(ms.Message, "General Error");
			}
			return CurrentNode;
		}

		private void treeViewRunc_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			if(e.Action != TreeViewAction.Unknown)
			{
				if(e.Node.Nodes.Count > 0)
				{
					this.CheckAllChildNodes(e.Node, e.Node.Checked);
				}
				else
				{
					if (e.Node.Parent!=null)
					{
						e.Node.Parent.Checked =true;
					}
				}
			}
		
		}


	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -