using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using CommonLib; using System.Text.RegularExpressions; namespace Lskj.MyControl { public partial class TreeViewEx : UserControl { public TreeViewEventHandler MyAfterSelect { get; set; } public TreeViewEx() { InitializeComponent(); tvMain.AfterCheck += new TreeViewEventHandler(tvLeft_AfterCheck); tvMain.AfterSelect += new TreeViewEventHandler(tvLeft_AfterSelect); } [Description("返回树")] private TreeView getTreeView; public TreeView GetTreeView { get { return tvMain; } set { getTreeView = value; } } #region 选择树 protected void tvLeft_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Action == TreeViewAction.ByMouse) { if (MyAfterSelect != null) MyAfterSelect(sender, e); } } #endregion #region 绑定树 protected void tvLeft_AfterCheck(object sender, TreeViewEventArgs e) { if (e.Action == TreeViewAction.ByMouse) { //取消节点选中状态之后,取消所有父节点的选中状态 if (e.Node.Checked) { setChildNodeCheckedState(e.Node, true); } else { //取消节点选中状态之后,取消所有父节点的选中状态 setChildNodeCheckedState(e.Node, false); //如果节点存在父节点,取消父节点的选中状态 if (e.Node.Parent != null) { setParentNodeCheckedState(e.Node, false); } } } } /// /// 取消节点选中状态之后,取消所有父节点的选中状态 /// /// /// private void setParentNodeCheckedState(TreeNode currNode, bool state) { TreeNode parentNode = currNode.Parent; parentNode.Checked = state; if (currNode.Parent.Parent != null) { setParentNodeCheckedState(currNode.Parent, state); } } /// /// 选中节点之后,选中节点的所有子节点 /// /// /// private void setChildNodeCheckedState(TreeNode currNode, bool state) { TreeNodeCollection nodes = currNode.Nodes; if (nodes.Count > 0) foreach (TreeNode tn in nodes) { tn.Checked = state; setChildNodeCheckedState(tn, state); } } #endregion /// /// 替换sql /// /// public static List GetParamValue(string param) { string SqlParamRegex = "{([^{])+}"; List list = new List(); Regex regex = new Regex(SqlParamRegex); MatchCollection mcs = regex.Matches(param); foreach (Match item in mcs) { if (list.IndexOf(item.Value) == -1) list.Add(item.Value); } return list; } public static string ReqSqlParam(string sql, string OperatorId, string OperatorName) { string sourceSql = sql; List listParm = GetParamValue(sql); foreach (string s in listParm) { if (s.IndexOf("loginid", StringComparison.OrdinalIgnoreCase) != -1) { sourceSql = sourceSql.Replace(s, OperatorId); } if (s.IndexOf("loginname", StringComparison.OrdinalIgnoreCase) != -1) { sourceSql = sourceSql.Replace(s, OperatorName); } if (s.IndexOf("groupid", StringComparison.OrdinalIgnoreCase) != -1) { sourceSql = sourceSql.Replace(s, ERPInfo.Instance.GroupId); } } return sourceSql; } #region 绑定树 /// /// 绑定树结构 /// /// /// public void BindTree(string DLLCoid) { DataTable dtTree = SqlHelper.ExecuteDataSet("select * from p_systemwordbooktab where tab='" + DLLCoid + "' and fieldsqltag=3").Tables[0]; if (dtTree != null && dtTree.Rows.Count > 0) { this.Tag = dtTree.Rows[0]; string treeSql = dtTree.Rows[0]["fieldsql"] + ""; treeSql = ReqSqlParam(treeSql,ERPInfo.Instance.EmployeeId.ToString(), ERPInfo.Instance.EmployeeName); DataTable dt = SqlHelper.ExecuteDataSet(treeSql).Tables[0]; for (int i = 0; i < dt.Rows.Count; i++) { DataRow dr = dt.Rows[i]; String tnKey = dr[dtTree.Rows[0]["fieldsqlid"] + ""] + ""; String tnStr = dr[dtTree.Rows[0]["fieldsqlname"] + ""] + ""; DataColumnCollection dcc = dt.Columns; String CodeNo = string.Empty; if (dcc.Count > 2) CodeNo = dr[2].ToString(); TreeNode[] tn = null; if (tnKey.Length >= 4) { String pKey = tnKey.Substring(0, tnKey.Length - 2); tn = tvMain.Nodes.Find(pKey, true); if (tn.Count() > 0) { TreeNode tNode = tn[0].Nodes.Add(tnKey, tnStr); tNode.Tag = CodeNo; } } else { TreeNode tNode = tvMain.Nodes.Add(tnKey, tnStr); } } } } private string checkValues = string.Empty; /// /// 选中节点之后,选中节点的所有子节点 /// /// /// private void GetCheckedState(TreeNode currNode) { if (currNode.Checked) checkValues += "'" + currNode.Name + "',"; TreeNodeCollection nodes = currNode.Nodes; if (nodes.Count > 0) { foreach (TreeNode tn in nodes) { if (tn.Checked) checkValues += "'" + tn.Name + "',"; GetCheckedState(tn); } } } private void GetCheckedStateWithNoQuote(TreeNode currNode) { if (currNode.Checked) checkValues += "" + currNode.Name + ","; TreeNodeCollection nodes = currNode.Nodes; if (nodes.Count > 0) { foreach (TreeNode tn in nodes) { if (tn.Checked) checkValues += "" + tn.Name + ","; GetCheckedState(tn); } } } /// /// 获取控件选择值 /// /// public string GetCheckValues() { checkValues = string.Empty; foreach (TreeNode tn in tvMain.Nodes) GetCheckedState(tn); return checkValues.Trim(','); } /// /// 获取控件选择值 /// /// public string GetCheckValuesWithNoQuote() { checkValues = string.Empty; foreach (TreeNode tn in tvMain.Nodes) GetCheckedStateWithNoQuote(tn); return checkValues.Trim(','); } /// /// 获取控件选择值 /// /// public string GetSelectValue() { checkValues = string.Empty; TreeNode currNode = tvMain.SelectedNode; if (currNode != null) { checkValues = currNode.Name; } return checkValues; } #endregion } }