1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054 |
- 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 System.Runtime.InteropServices;
- using System.Collections;
- namespace LYFZ.ComponentLibrary
- {
- public partial class ComboBoxTreeViewEx : ComboBox
- {
- #region 只读
- [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
- public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
- int GW_CHILD = 5;
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- //wParam=1为只读;wParam=0为可写
- public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
- public const int EM_SETREADONLY = 0xcf;
- private bool _readonly = false;
- /// <summary>
- /// 只读
- /// </summary>
- public bool ReadOnly
- {
- get { return _readonly; }
- set
- {
- _readonly = value;
- if (_readonly)
- {
- IntPtr editHandle = GetWindow(this.Handle, GW_CHILD);
- SendMessage(editHandle, EM_SETREADONLY, 1, 0);
- }
- else
- {
- IntPtr editHandle = GetWindow(this.Handle, GW_CHILD);
- SendMessage(editHandle, EM_SETREADONLY, 0, 0);
- }
- }
- }
- #endregion
- private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
- ToolStripControlHost treeViewHost;
- ToolStripDropDown dropDown;
- TextBoxEx textBox = new TextBoxEx();
- ToolStripControlHost toolsTextBoxHost;
- TreeView treeView = new TreeView();
- public ComboBoxTreeViewEx()
- {
- treeView.AfterSelect += new TreeViewEventHandler(treeView_AfterSelect);
- treeView.BeforeExpand += new TreeViewCancelEventHandler(treeView_BeforeExpand);
- treeView.BorderStyle = BorderStyle.None;
- treeViewHost = new ToolStripControlHost(treeView);
- dropDown = new ToolStripDropDown();
- dropDown.Width = this.Width;
- textBox.BaseText.TextChanged += TextBox_TextChanged;
- toolsTextBoxHost = new ToolStripControlHost(textBox);
- toolsTextBoxHost.Dock = DockStyle.Top;
-
- dropDown.Items.Add(toolsTextBoxHost);
- dropDown.Items.Add(treeViewHost);
- this.DropDownHeight = 220;
- dropDown.Closed += DropDown_Closed;
-
- treeView.NodeMouseClick += treeView_NodeMouseClick;
- }
- private void DropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e)
- {
- this.textBox.Text = "";
- }
- bool isTextsearch = false;
- private void TextBox_TextChanged(object sender, EventArgs e)
- {
- isTextsearch = true;
- treeView.ExpandAll();
- if (String.IsNullOrWhiteSpace(this.textBox.Text.ToLower().Trim()))
- {
- treeView.CollapseAll();
- if (treeView.Nodes.Count > 0)
- {
- treeView.Nodes[0].EnsureVisible();
- treeView.Nodes[0].ExpandAll();
- }
- }
- foreach (TreeNode item in treeView.Nodes)
- {
- item.BackColor = Color.White;
-
- if ((!String.IsNullOrWhiteSpace(this.textBox.Text.ToLower().Trim()) &&item.Text.ToLower().Contains(this.textBox.Text.ToLower().Trim()))
- || (!String.IsNullOrWhiteSpace(this.textBox.Text.ToLower().Trim()) && LYFZ.Command.Command_ChineseToPinyin.GetFirstPYLetter(item.Text).ToLower().Contains(this.textBox.Text.ToLower().Trim())))
- {
- item.EnsureVisible();
- item.BackColor = Color.Green;
- if (item.Nodes.Count > 0)
- {
- SearchNode(item, this.textBox.Text);
- }
- }
- else {
- if (item.Nodes.Count > 0)
- {
- SearchNode(item, this.textBox.Text);
- }
- }
- }
- isTextsearch = false;
- }
- void SearchNode(TreeNode treeNode,string key)
- {
- foreach (TreeNode item in treeNode.Nodes)
- {
- item.BackColor = Color.White;
- if ((!String.IsNullOrWhiteSpace(key) && item.Text.ToLower().Contains(key.ToLower().Trim()))
- || (!String.IsNullOrWhiteSpace(key) && LYFZ.Command.Command_ChineseToPinyin.GetFirstPYLetter(item.Text).ToLower().Contains(key.ToLower().Trim())))
- {
-
- item.EnsureVisible();
- item.BackColor = Color.Green;
- if (item.Nodes.Count > 0)
- {
- SearchNode(item, this.textBox.Text);
- }
- }
- else
- {
- if (item.Nodes.Count > 0)
- {
- SearchNode(item, this.textBox.Text);
- }
- }
- }
- }
- bool isNodeMouseClick = false;
- /// <summary>
- /// 是否开启节点单击事件
- /// </summary>
- [Category("控件扩展事件"), Description("是否开启节点单击事件")]
- public bool IsNodeMouseClick
- {
- get { return isNodeMouseClick; }
- set { isNodeMouseClick = value; }
- }
- //定义delegate
- public delegate void ComboBoxTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e);
- //用event 关键字声明事件对象
- /// <summary>
- ///
- /// </summary>
- [Category("控件扩展事件"), Description("文本框值修改时")]
- public event ComboBoxTreeView_NodeMouseClick ComboBoxTree_NodeMouseClick;
- void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
- {
- if (!IsNodeMouseClick) { return; }
- if (!this.AfterSelectLevelFirstNode)
- {
- if (e.Node.Level == 0)
- { return; }
- }
- if (e.Node.Nodes.Count <= 0 || (this.isSelectParentNode && e.X > (e.Node.Level + 1) * 20))//判断选中节点是否有子节点
- {
- this.Text = "";
- this.StrGetName = e.Node.Name.ToString();
- this.Tag = e.Node.Tag;
- this.Text = e.Node.Text.ToString();
- dropDown.Close();
- if (this.ComboBoxTree_NodeMouseClick != null)
- { ComboBoxTree_NodeMouseClick(sender, e); }
- }
- }
- [Browsable(true), Description("获取TreeView的节点集合"), Category("TreeView"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(typeof(TreeNodeCollection), typeof(TreeNodeCollection))]
- public TreeNodeCollection Nodes
- {
- get
- { return this.TreeView.Nodes; }
- }
- [Browsable(true), Description("获取或设置TreeView的选定节点"), Category("TreeView")]
- public TreeNode SelectedNode
- {
- set
- { this.TreeView.SelectedNode = value; }
- get
- { return this.TreeView.SelectedNode; }
- }
- private object _strkey;
- /// <summary>
- ///
- /// </summary>
- public object StrKey
- {
- set { _strkey = value; }
- get { return _strkey; }
- }
- bool isSelectParentNode = false;
- /// <summary>
- /// 是否允许选择父节点
- /// </summary>
- [Category("控件扩展事件"), Description("是否允许选择父节点")]
- public bool IsSelectParentNode
- {
- get { return isSelectParentNode; }
- set { isSelectParentNode = value; }
- }
- public TreeView TreeView
- {
- get { return treeViewHost.Control as TreeView; }
- }
- private void ShowDropDown()
- {
- if (dropDown != null)
- {
- treeViewHost.Size = new Size(DropDownWidth - 2, DropDownHeight);
- dropDown.Show(this, 0, this.Height);
- }
- }
- protected override void WndProc(ref Message m)
- {
- if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
- {
- ShowDropDown();
- return;
- }
- base.WndProc(ref m);
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (dropDown != null)
- {
- dropDown.Dispose();
- dropDown = null;
- }
- }
- base.Dispose(disposing);
- }
- #region 定义文本框值更改后触发事件
- //定义delegate
- public delegate void ComboBoxTreeView_TextChanged(object sender, TreeViewEventArgs e);
- //用event 关键字声明事件对象
- /// <summary>
- ///
- /// </summary>
- [Category("控件扩展事件"), Description("文本框值修改时")]
- public event ComboBoxTreeView_TextChanged ComboBoxTreeViewEx_TextChanged;
- private bool _dropdownshow = false;
- /// <summary>
- /// 控件下拉是展开还是隐藏
- /// </summary>
- public bool DropDownShow
- {
- get { return _dropdownshow; }
- set { _dropdownshow = value; }
- }
- private bool _afterselectlevelfirstnode;
- /// <summary>
- /// 是否可选择第一个节点,true:可以选择,false:不能选择
- /// </summary>
- [Category("控件扩展事件"), Description("是否可选择第一个节点,true:可以选择,false:不能选择")]
- public bool AfterSelectLevelFirstNode
- {
- get { return _afterselectlevelfirstnode; }
- set { _afterselectlevelfirstnode = value; }
- }
- /// <summary>
- /// 是否设置为空,未选择
- /// </summary>
- bool isSetValueNull = false;
- /// <summary>
- /// 清空当前控件所选择的值
- /// </summary>
- public void SetTextAndTag_ValueNull()
- {
- this.Text = "";
- this.Tag = null;
- this.StrGetName = "";
- this.isSetValueNull = true;
- this.NodeClearBackColor();
- }
- private string _StrGetName = "";
- /// <summary>
- /// 获取Tag同等的Name
- /// </summary>
- public string StrGetName
- {
- get { return _StrGetName; }
- set { _StrGetName = value; }
- }
- private string _StrGetTagName = "";
- /// <summary>
- /// 获取控件TagName同等的其他控件的Tag
- /// </summary>
- public string StrGetTagName
- {
- get { return _StrGetTagName; }
- set { _StrGetTagName = value; }
- }
- /// <summary>
- /// 节点选择事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
- {
- if (this.isSetValueNull) { this.isSetValueNull = false; TreeView.SelectedNode.ForeColor = Color.Red; return; }
- if (e.Node.Nodes.Count <= 0 || this.isSelectParentNode)//判断选中节点是否有子节点
- {
- NodeClearBackColor();
- if (AfterSelectLevelFirstNode)
- {
- this.StrGetName = TreeView.SelectedNode.Name.ToString();
- this.Tag = TreeView.SelectedNode.Tag;
- this.Text = TreeView.SelectedNode.Text.ToString();
- TreeView.SelectedNode.ForeColor = Color.Red;
- dropDown.Close();
- }
- else
- {
- if (DropDownShow && e.Node.Level == 0)
- {
- if (e.Node.Level == 0 && e.Node.Text == "")
- {
- this.Tag = null;
- this.Text = "";
- dropDown.Close();
- }
- else
- {
- this.StrGetName = "";
- this.Text = "";
- dropDown.Close();
- }
- }
- else
- {
- if (TreeView.SelectedNode.Name.ToString().Trim() != "")
- {
- this.StrGetName = TreeView.SelectedNode.Name.ToString();
- this.Tag = TreeView.SelectedNode.Tag;
- this.Text = TreeView.SelectedNode.Text.ToString();
- TreeView.SelectedNode.ForeColor = Color.Red;
- dropDown.Close();
- }
- else
- {
- if (TreeView.SelectedNode.Text.ToString().Trim() == "")
- {
- this.StrGetName = TreeView.SelectedNode.Name.ToString();
- this.Tag = TreeView.SelectedNode.Tag;
- this.Text = TreeView.SelectedNode.Text.ToString();
- TreeView.SelectedNode.ForeColor = Color.Red;
- dropDown.Close();
- }
- else
- {
- return;
- }
- }
- }
- }
- }
- if (this.ComboBoxTreeViewEx_TextChanged != null)
- ComboBoxTreeViewEx_TextChanged(sender, e);
- //选择后,关闭所有展开的节点
- /* foreach (TreeNode item in ((TreeView)sender).Nodes)
- {
- if (e.Node != item)
- {
- item.Collapse();
- }
- }*/
- }
- /// <summary>
- /// 点击一个节点展开,关闭其他展开的节点
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void treeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
- {
- if (isTextsearch)
- {
- return;
- }
- foreach (TreeNode item in ((TreeView)sender).Nodes)
- {
- if (e.Node != item && !item.Nodes.Contains(e.Node))
- {
- item.Collapse();
- }
- }
- }
- #endregion
- #region 根据Tag获取当前所指的Text
- /// <summary>
- /// 遍历树,根据Tag获取当前所指的Text
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void TagFindText(object ObjKey)
- {
- IsTagReturn = false;
- NodeClearBackColor();
- foreach (TreeNode node in this.TreeView.Nodes)
- {
- if (node.Tag != null)
- {
- if (node.Tag.Equals(ObjKey))
- {
- this.Text = node.Text;
- this.Tag = node.Tag;
- this.StrGetName = node.Name;
- //node.BackColor = System.Drawing.Color.FromArgb(224, 224, 224); //Color.LightPink;
- this.TreeView.SelectedNode = node;
- node.ForeColor = Color.Red;
- IsTagReturn = true;
- }
- else
- { TagForeachTreeNode(node, ObjKey); }
- }
- else
- { TagForeachTreeNode(node, ObjKey); }
- if (IsTagReturn)
- { return; }
- }
- }
- private bool IsTagReturn = false;
- /// <summary>
- /// 循环遍历子节点
- /// </summary>
- private void TagForeachTreeNode(TreeNode node, object Key)
- {
- foreach (TreeNode nde in node.Nodes)
- {
- if (nde.Tag.Equals(Key))
- {
- this.Text = nde.Text;
- this.Tag = nde.Tag;
- this.StrGetName = nde.Name;
- //nde.BackColor = System.Drawing.Color.FromArgb(224, 224, 224); //Color.Silver;
- this.TreeView.SelectedNode = nde;
- nde.ForeColor = Color.Red;
- node.ExpandAll();
- IsTagReturn = true;
- return;
- }
- else
- {
- if (nde.Nodes.Count > 0)
- {
- TagForeachTreeNode(nde, Key);
- }
- }
- }
- }
- #endregion
- #region 根据Text获取当前所指的Tag
- /// <summary>
- /// 遍历树,根据Text获取当前所指的Tag
- /// </summary>
- /// <param name="ObjText"></param>
- public void TextFindTag(string ObjText)
- {
- if (ObjText == null)
- {
- ObjText = "";
- }
- IsTextReturn = false;
- NodeClearBackColor();
- foreach (TreeNode node in TreeView.Nodes)
- {
- if (node.Tag != null)
- {
- if (node.Text.Equals(ObjText))
- {
- this.Text = node.Text;
- this.Tag = node.Tag;
- this.StrGetName = node.Name;
- this.TreeView.SelectedNode = node;
- node.ForeColor = Color.Red;
- IsTextReturn = true;
- }
- else
- { TextForeachTreeNode(node, ObjText); }
- }
- else
- { TextForeachTreeNode(node, ObjText); }
- if (IsTextReturn)
- { return; }
- }
- this.Text = ObjText.Trim();
- }
- private bool IsTextReturn = false;
- /// <summary>
- /// 循环遍历子节点
- /// </summary>
- private void TextForeachTreeNode(TreeNode node, string ObjText)
- {
- foreach (TreeNode nde in node.Nodes)
- {
- if (nde.Text.Equals(ObjText))
- {
- this.Text = nde.Text;
- this.Tag = nde.Tag;
- this.StrGetName = nde.Name;
- this.TreeView.SelectedNode = node;
- nde.ForeColor = Color.Red;
- node.Expand();
- if (node.Level > 0)
- { node.Parent.Expand(); }
- IsTextReturn = true;
- return;
- }
- else
- {
- if (nde.Nodes.Count > 0)
- { TextForeachTreeNode(nde, ObjText); }
- }
- }
- }
- /// <summary>
- /// 遍历树,根据Text获取当前所指的Tag
- /// </summary>
- /// <param name="ObjText"></param>
- public string TextFindParentText(string ObjText)
- {
- bool Isbl = false;
- string StrText = "";
- foreach (TreeNode node in TreeView.Nodes)
- {
- if (node.Tag != null)
- {
- if (node.Text.Equals(ObjText))
- {
- StrText = node.Text; Isbl = true;
- }
- else
- { StrText = TextForeachTreeNodeParentText(node, ObjText, ref Isbl); }
- }
- else
- { StrText = TextForeachTreeNodeParentText(node, ObjText, ref Isbl); }
- if (Isbl)
- { break; }
- }
- return StrText;
- }
- /// <summary>
- /// 循环遍历子节点
- /// </summary>
- private string TextForeachTreeNodeParentText(TreeNode node, string ObjText, ref bool Isbl)
- {
- string strText = "";
- foreach (TreeNode nde in node.Nodes)
- {
- if (nde.Text.Equals(ObjText))
- {
- strText = nde.Parent.Text;
- Isbl = true;
- }
- else
- {
- if (nde.Nodes.Count > 0)
- { strText = TextForeachTreeNodeParentText(nde, ObjText, ref Isbl); }
- }
- if (Isbl)
- { break; }
- }
- return strText;
- }
- #endregion
- #region 获取当前选择的最前的父节点
- /// <summary>
- /// 遍历树,根据Text获取当前所指的Tag
- /// </summary>
- /// <param name="ObjText"></param>
- public Hashtable TextFindParentIsParent(string ObjText)
- {
- bool Isbl = false;
- Hashtable htData = new Hashtable();
- if (ObjText != "")
- {
- foreach (TreeNode node in TreeView.Nodes)
- {
- if (node.Tag != null)
- {
- if (node.Text.Equals(ObjText))
- {
- htData = GetNodeParent(node);
- Isbl = true;
- }
- else
- { htData = TextForeachTreeNodeParentIsParent(node, ObjText, ref Isbl); }
- }
- else
- { htData = TextForeachTreeNodeParentIsParent(node, ObjText, ref Isbl); }
- if (Isbl)
- { break; }
- }
- }
- if (htData.Count <= 0)
- {
- htData["Text"] = "";
- htData["Tag"] = "";
- htData["Name"] = "";
- }
- return htData;
- }
- /// <summary>
- /// 循环遍历子节点
- /// </summary>
- private Hashtable TextForeachTreeNodeParentIsParent(TreeNode node, string ObjText, ref bool Isbl)
- {
- Hashtable htData = new Hashtable();
- foreach (TreeNode nde in node.Nodes)
- {
- if (nde.Text.Equals(ObjText))
- {
- htData = GetNodeParent(node);
- Isbl = true;
- }
- else
- {
- if (nde.Nodes.Count > 0)
- { htData = TextForeachTreeNodeParentIsParent(nde, ObjText, ref Isbl); }
- }
- if (Isbl)
- { break; }
- }
- return htData;
- }
- /// <summary>
- /// 获取当前选择的最前的父节点
- /// </summary>
- /// <param name="node"></param>
- /// <returns></returns>
- private Hashtable GetNodeParent(TreeNode node)
- {
- Hashtable htData = new Hashtable();
- if (node != null)
- {
- bool Isbl = true;
- while (Isbl)
- {
- if (node.Parent != null)
- {
- node = node.Parent;
- }
- else
- {
- htData["Text"] = node.Text.Trim();
- htData["Tag"] = node.Tag.ToString().Trim();
- htData["Name"] = node.Name.Trim();
- Isbl = false;
- }
- }
- }
- return htData;
- }
- #endregion
- #region 获取当前选择的最前的父节点
- /// <summary>
- /// 遍历树,根据Text获取当前所指的Tag
- /// </summary>
- /// <param name="ObjText"></param>
- public Hashtable TagFindParentIsParent(string ObjTag)
- {
- bool Isbl = false;
- Hashtable htData = new Hashtable();
- if (ObjTag != "")
- {
- foreach (TreeNode node in TreeView.Nodes)
- {
- if (node.Tag != null)
- {
- if (node.ToString().Trim().Equals(ObjTag))
- {
- htData = TagGetNodeParent(node);
- Isbl = true;
- }
- else
- { htData = TagForeachTreeNodeParentIsParent(node, ObjTag, ref Isbl); }
- }
- else
- { htData = TagForeachTreeNodeParentIsParent(node, ObjTag, ref Isbl); }
- if (Isbl)
- { break; }
- }
- }
- if (htData.Count <= 0)
- {
- htData["Text"] = "";
- htData["Tag"] = "";
- htData["Name"] = "";
- }
- return htData;
- }
- /// <summary>
- /// 循环遍历子节点
- /// </summary>
- private Hashtable TagForeachTreeNodeParentIsParent(TreeNode node, string ObjTag, ref bool Isbl)
- {
- Hashtable htData = new Hashtable();
- foreach (TreeNode nde in node.Nodes)
- {
- if (nde.Tag.ToString().Trim().Equals(ObjTag))
- {
- htData = TagGetNodeParent(node);
- Isbl = true;
- }
- else
- {
- if (nde.Nodes.Count > 0)
- { htData = TagForeachTreeNodeParentIsParent(nde, ObjTag, ref Isbl); }
- }
- if (Isbl)
- { break; }
- }
- return htData;
- }
- /// <summary>
- /// 获取当前选择的最前的父节点
- /// </summary>
- /// <param name="node"></param>
- /// <returns></returns>
- private Hashtable TagGetNodeParent(TreeNode node)
- {
- Hashtable htData = new Hashtable();
- if (node != null)
- {
- bool Isbl = true;
- while (Isbl)
- {
- if (node.Parent != null)
- {
- node = node.Parent;
- }
- else
- {
- htData["Text"] = node.Text.Trim();
- htData["Tag"] = node.Tag.ToString().Trim();
- htData["Name"] = node.Name.Trim();
- Isbl = false;
- }
- }
- }
- return htData;
- }
- #endregion
- /// <summary>
- /// 清除之前所选中的项的背景颜色
- /// </summary>
- /// <param name="node"></param>
- private void NodeClearBackColor()
- {
- foreach (TreeNode node in TreeView.Nodes)
- {
- //node.BackColor = Color.White;
- node.ForeColor = Color.Black;
- if (node.Nodes.Count > 0)
- { NodeClearBackColor(node); }
- }
- }
- /// <summary>
- /// 清除节点下的所有节点的背景颜色
- /// </summary>
- /// <param name="node"></param>
- private void NodeClearBackColor(TreeNode node)
- {
- foreach (TreeNode nde in node.Nodes)
- {
- //nde.BackColor = Color.White;
- nde.ForeColor = Color.Black;
- if (nde.Nodes.Count > 0)
- {
- NodeClearBackColor(nde);
- if (nde.Nodes.Count > 0)
- { NodeClearBackColor(nde); }
- }
- }
- }
- int _Nodelevel = 0;
- /// <summary>
- /// 无效属性
- /// </summary>
- [Obsolete("此属性过期不建议使用")]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
- public int Nodelevel
- {
- get { return _Nodelevel; }
- set { _Nodelevel = value; }
- }
- public TreeNode GetNodeItem_TextToTag(string ObjText)
- {
- foreach (TreeNode node in TreeView.Nodes)
- {
- if (node.Tag != null)
- {
- if (node.Text.Equals(ObjText))
- {
- return node;
- }
- else if (node.Nodes.Count > 0)
- {
- foreach (TreeNode node2 in node.Nodes)
- {
- if (node2.Tag != null)
- {
- if (node2.Text.Equals(ObjText))
- { return node2; }
- else if (node2.Nodes.Count > 0)
- {
- foreach (TreeNode node3 in node2.Nodes)
- {
- if (node3.Tag != null)
- {
- if (node3.Text.Equals(ObjText))
- { return node3; }
- }
- }
- }
- }
- }
- }
- }
- }
- return null;
- }
- public TreeNode GetNodeItem_TagToText(string ObjTag)
- {
- foreach (TreeNode node in TreeView.Nodes)
- {
- if (node.Tag != null)
- {
- if (node.Tag.Equals(ObjTag))
- {
- return node;
- }
- else if (node.Nodes.Count > 0)
- {
- foreach (TreeNode node2 in node.Nodes)
- {
- if (node2.Tag != null)
- {
- if (node2.Tag.Equals(ObjTag))
- { return node2; }
- else if (node2.Nodes.Count > 0)
- {
- foreach (TreeNode node3 in node2.Nodes)
- {
- if (node3.Tag != null)
- {
- if (node3.Tag.Equals(ObjTag))
- { return node3; }
- }
- }
- }
- }
- }
- }
- }
- }
- return null;
- }
- public string GetSelectedNodeName()
- {
- if (this.TreeView.SelectedNode != null)
- {
- return this.TreeView.SelectedNode.Name;
- }
- else {
- return "";
- }
- }
- public string GetSelectedNodeText()
- {
- if (this.TreeView.SelectedNode != null)
- {
- return this.TreeView.SelectedNode.Text;
- }
- else
- {
- return "";
- }
- }
- /// <summary>
- /// 设置选择项
- /// </summary>
- /// <param name="selectValue">要选中的值</param>
- /// <param name="isDisabled">是否禁用</param>
- public void SetNodeNameToSelectItme(string selectValue, bool isDisabled = false)
- {
- if (string.IsNullOrEmpty(selectValue))
- {
- this.TreeView.SelectedNode = null;
- this.Text = "";
- }
- else
- {
- foreach (TreeNode node in this.TreeView.Nodes)
- {
- if (node.Name.ToLower() == selectValue.ToLower())
- {
- this.TreeView.SelectedNode = node;
- this.Text = node.Text;
- break;
- }
- else if (node.Nodes.Count > 0)
- {
- NodeNameToSelectSubItem(node, selectValue);
- }
- }
- }
- if (isDisabled)
- {
- this.Enabled = false;
- }
- else
- {
- this.Enabled = true;
- }
- }
- public void NodeNameToSelectSubItem(TreeNode node, string selectValue)
- {
- foreach (TreeNode subNode in node.Nodes)
- {
- if (subNode.Name.ToLower() == selectValue.ToLower())
- {
- this.TreeView.SelectedNode = subNode;
- this.SelectedText = subNode.Text;
- break;
- }
- else if (subNode.Nodes.Count > 0)
- {
- NodeNameToSelectSubItem( subNode, selectValue);
- }
- }
- }
- /// <summary>
- /// 设置选择项
- /// </summary>
- /// <param name="cmbProject"></param>
- /// <param name="selectValue">要选中的值</param>
- /// <param name="isDisabled">是否禁用</param>
- public void SetNodeTextToSelectItme(string selectValue, bool isDisabled = false)
- {
- if (string.IsNullOrEmpty(selectValue))
- {
- this.TreeView.SelectedNode = null;
- this.Text = "";
- }
- else
- {
- foreach (TreeNode node in this.TreeView.Nodes)
- {
- if (node.Text.ToLower() == selectValue.ToLower())
- {
- this.TreeView.SelectedNode = node;
- this.Text = node.Text;
- break;
- }
- else if (node.Nodes.Count > 0)
- {
- NodeTextToSelectSubItem(node, selectValue);
- }
- }
- }
- if (isDisabled)
- {
- this.Enabled = false;
- }
- else
- {
- this.Enabled = true;
- }
- }
- public void NodeTextToSelectSubItem(TreeNode node, string selectValue)
- {
- foreach (TreeNode subNode in node.Nodes)
- {
- if (subNode.Text.ToLower() == selectValue.ToLower())
- {
- this.TreeView.SelectedNode = subNode;
- this.SelectedText = subNode.Text;
- break;
- }
- else if (subNode.Nodes.Count > 0)
- {
- NodeTextToSelectSubItem( subNode, selectValue);
- }
- }
- }
- }
- }
|