ComboBoxTreeViewEx.cs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;
  10. using System.Collections;
  11. namespace LYFZ.ComponentLibrary
  12. {
  13. public partial class ComboBoxTreeViewEx : ComboBox
  14. {
  15. #region 只读
  16. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  17. public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
  18. int GW_CHILD = 5;
  19. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  20. //wParam=1为只读;wParam=0为可写
  21. public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
  22. public const int EM_SETREADONLY = 0xcf;
  23. private bool _readonly = false;
  24. /// <summary>
  25. /// 只读
  26. /// </summary>
  27. public bool ReadOnly
  28. {
  29. get { return _readonly; }
  30. set
  31. {
  32. _readonly = value;
  33. if (_readonly)
  34. {
  35. IntPtr editHandle = GetWindow(this.Handle, GW_CHILD);
  36. SendMessage(editHandle, EM_SETREADONLY, 1, 0);
  37. }
  38. else
  39. {
  40. IntPtr editHandle = GetWindow(this.Handle, GW_CHILD);
  41. SendMessage(editHandle, EM_SETREADONLY, 0, 0);
  42. }
  43. }
  44. }
  45. #endregion
  46. private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
  47. ToolStripControlHost treeViewHost;
  48. ToolStripDropDown dropDown;
  49. TextBoxEx textBox = new TextBoxEx();
  50. ToolStripControlHost toolsTextBoxHost;
  51. TreeView treeView = new TreeView();
  52. public ComboBoxTreeViewEx()
  53. {
  54. treeView.AfterSelect += new TreeViewEventHandler(treeView_AfterSelect);
  55. treeView.BeforeExpand += new TreeViewCancelEventHandler(treeView_BeforeExpand);
  56. treeView.BorderStyle = BorderStyle.None;
  57. treeViewHost = new ToolStripControlHost(treeView);
  58. dropDown = new ToolStripDropDown();
  59. dropDown.Width = this.Width;
  60. textBox.BaseText.TextChanged += TextBox_TextChanged;
  61. toolsTextBoxHost = new ToolStripControlHost(textBox);
  62. toolsTextBoxHost.Dock = DockStyle.Top;
  63. dropDown.Items.Add(toolsTextBoxHost);
  64. dropDown.Items.Add(treeViewHost);
  65. this.DropDownHeight = 220;
  66. dropDown.Closed += DropDown_Closed;
  67. treeView.NodeMouseClick += treeView_NodeMouseClick;
  68. }
  69. private void DropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e)
  70. {
  71. this.textBox.Text = "";
  72. }
  73. bool isTextsearch = false;
  74. private void TextBox_TextChanged(object sender, EventArgs e)
  75. {
  76. isTextsearch = true;
  77. treeView.ExpandAll();
  78. if (String.IsNullOrWhiteSpace(this.textBox.Text.ToLower().Trim()))
  79. {
  80. treeView.CollapseAll();
  81. if (treeView.Nodes.Count > 0)
  82. {
  83. treeView.Nodes[0].EnsureVisible();
  84. treeView.Nodes[0].ExpandAll();
  85. }
  86. }
  87. foreach (TreeNode item in treeView.Nodes)
  88. {
  89. item.BackColor = Color.White;
  90. if ((!String.IsNullOrWhiteSpace(this.textBox.Text.ToLower().Trim()) &&item.Text.ToLower().Contains(this.textBox.Text.ToLower().Trim()))
  91. || (!String.IsNullOrWhiteSpace(this.textBox.Text.ToLower().Trim()) && LYFZ.Command.Command_ChineseToPinyin.GetFirstPYLetter(item.Text).ToLower().Contains(this.textBox.Text.ToLower().Trim())))
  92. {
  93. item.EnsureVisible();
  94. item.BackColor = Color.Green;
  95. if (item.Nodes.Count > 0)
  96. {
  97. SearchNode(item, this.textBox.Text);
  98. }
  99. }
  100. else {
  101. if (item.Nodes.Count > 0)
  102. {
  103. SearchNode(item, this.textBox.Text);
  104. }
  105. }
  106. }
  107. isTextsearch = false;
  108. }
  109. void SearchNode(TreeNode treeNode,string key)
  110. {
  111. foreach (TreeNode item in treeNode.Nodes)
  112. {
  113. item.BackColor = Color.White;
  114. if ((!String.IsNullOrWhiteSpace(key) && item.Text.ToLower().Contains(key.ToLower().Trim()))
  115. || (!String.IsNullOrWhiteSpace(key) && LYFZ.Command.Command_ChineseToPinyin.GetFirstPYLetter(item.Text).ToLower().Contains(key.ToLower().Trim())))
  116. {
  117. item.EnsureVisible();
  118. item.BackColor = Color.Green;
  119. if (item.Nodes.Count > 0)
  120. {
  121. SearchNode(item, this.textBox.Text);
  122. }
  123. }
  124. else
  125. {
  126. if (item.Nodes.Count > 0)
  127. {
  128. SearchNode(item, this.textBox.Text);
  129. }
  130. }
  131. }
  132. }
  133. bool isNodeMouseClick = false;
  134. /// <summary>
  135. /// 是否开启节点单击事件
  136. /// </summary>
  137. [Category("控件扩展事件"), Description("是否开启节点单击事件")]
  138. public bool IsNodeMouseClick
  139. {
  140. get { return isNodeMouseClick; }
  141. set { isNodeMouseClick = value; }
  142. }
  143. //定义delegate
  144. public delegate void ComboBoxTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e);
  145. //用event 关键字声明事件对象
  146. /// <summary>
  147. ///
  148. /// </summary>
  149. [Category("控件扩展事件"), Description("文本框值修改时")]
  150. public event ComboBoxTreeView_NodeMouseClick ComboBoxTree_NodeMouseClick;
  151. void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
  152. {
  153. if (!IsNodeMouseClick) { return; }
  154. if (!this.AfterSelectLevelFirstNode)
  155. {
  156. if (e.Node.Level == 0)
  157. { return; }
  158. }
  159. if (e.Node.Nodes.Count <= 0 || (this.isSelectParentNode && e.X > (e.Node.Level + 1) * 20))//判断选中节点是否有子节点
  160. {
  161. this.Text = "";
  162. this.StrGetName = e.Node.Name.ToString();
  163. this.Tag = e.Node.Tag;
  164. this.Text = e.Node.Text.ToString();
  165. dropDown.Close();
  166. if (this.ComboBoxTree_NodeMouseClick != null)
  167. { ComboBoxTree_NodeMouseClick(sender, e); }
  168. }
  169. }
  170. [Browsable(true), Description("获取TreeView的节点集合"), Category("TreeView"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(typeof(TreeNodeCollection), typeof(TreeNodeCollection))]
  171. public TreeNodeCollection Nodes
  172. {
  173. get
  174. { return this.TreeView.Nodes; }
  175. }
  176. [Browsable(true), Description("获取或设置TreeView的选定节点"), Category("TreeView")]
  177. public TreeNode SelectedNode
  178. {
  179. set
  180. { this.TreeView.SelectedNode = value; }
  181. get
  182. { return this.TreeView.SelectedNode; }
  183. }
  184. private object _strkey;
  185. /// <summary>
  186. ///
  187. /// </summary>
  188. public object StrKey
  189. {
  190. set { _strkey = value; }
  191. get { return _strkey; }
  192. }
  193. bool isSelectParentNode = false;
  194. /// <summary>
  195. /// 是否允许选择父节点
  196. /// </summary>
  197. [Category("控件扩展事件"), Description("是否允许选择父节点")]
  198. public bool IsSelectParentNode
  199. {
  200. get { return isSelectParentNode; }
  201. set { isSelectParentNode = value; }
  202. }
  203. public TreeView TreeView
  204. {
  205. get { return treeViewHost.Control as TreeView; }
  206. }
  207. private void ShowDropDown()
  208. {
  209. if (dropDown != null)
  210. {
  211. treeViewHost.Size = new Size(DropDownWidth - 2, DropDownHeight);
  212. dropDown.Show(this, 0, this.Height);
  213. }
  214. }
  215. protected override void WndProc(ref Message m)
  216. {
  217. if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
  218. {
  219. ShowDropDown();
  220. return;
  221. }
  222. base.WndProc(ref m);
  223. }
  224. protected override void Dispose(bool disposing)
  225. {
  226. if (disposing)
  227. {
  228. if (dropDown != null)
  229. {
  230. dropDown.Dispose();
  231. dropDown = null;
  232. }
  233. }
  234. base.Dispose(disposing);
  235. }
  236. #region 定义文本框值更改后触发事件
  237. //定义delegate
  238. public delegate void ComboBoxTreeView_TextChanged(object sender, TreeViewEventArgs e);
  239. //用event 关键字声明事件对象
  240. /// <summary>
  241. ///
  242. /// </summary>
  243. [Category("控件扩展事件"), Description("文本框值修改时")]
  244. public event ComboBoxTreeView_TextChanged ComboBoxTreeViewEx_TextChanged;
  245. private bool _dropdownshow = false;
  246. /// <summary>
  247. /// 控件下拉是展开还是隐藏
  248. /// </summary>
  249. public bool DropDownShow
  250. {
  251. get { return _dropdownshow; }
  252. set { _dropdownshow = value; }
  253. }
  254. private bool _afterselectlevelfirstnode;
  255. /// <summary>
  256. /// 是否可选择第一个节点,true:可以选择,false:不能选择
  257. /// </summary>
  258. [Category("控件扩展事件"), Description("是否可选择第一个节点,true:可以选择,false:不能选择")]
  259. public bool AfterSelectLevelFirstNode
  260. {
  261. get { return _afterselectlevelfirstnode; }
  262. set { _afterselectlevelfirstnode = value; }
  263. }
  264. /// <summary>
  265. /// 是否设置为空,未选择
  266. /// </summary>
  267. bool isSetValueNull = false;
  268. /// <summary>
  269. /// 清空当前控件所选择的值
  270. /// </summary>
  271. public void SetTextAndTag_ValueNull()
  272. {
  273. this.Text = "";
  274. this.Tag = null;
  275. this.StrGetName = "";
  276. this.isSetValueNull = true;
  277. this.NodeClearBackColor();
  278. }
  279. private string _StrGetName = "";
  280. /// <summary>
  281. /// 获取Tag同等的Name
  282. /// </summary>
  283. public string StrGetName
  284. {
  285. get { return _StrGetName; }
  286. set { _StrGetName = value; }
  287. }
  288. private string _StrGetTagName = "";
  289. /// <summary>
  290. /// 获取控件TagName同等的其他控件的Tag
  291. /// </summary>
  292. public string StrGetTagName
  293. {
  294. get { return _StrGetTagName; }
  295. set { _StrGetTagName = value; }
  296. }
  297. /// <summary>
  298. /// 节点选择事件
  299. /// </summary>
  300. /// <param name="sender"></param>
  301. /// <param name="e"></param>
  302. private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
  303. {
  304. if (this.isSetValueNull) { this.isSetValueNull = false; TreeView.SelectedNode.ForeColor = Color.Red; return; }
  305. if (e.Node.Nodes.Count <= 0 || this.isSelectParentNode)//判断选中节点是否有子节点
  306. {
  307. NodeClearBackColor();
  308. if (AfterSelectLevelFirstNode)
  309. {
  310. this.StrGetName = TreeView.SelectedNode.Name.ToString();
  311. this.Tag = TreeView.SelectedNode.Tag;
  312. this.Text = TreeView.SelectedNode.Text.ToString();
  313. TreeView.SelectedNode.ForeColor = Color.Red;
  314. dropDown.Close();
  315. }
  316. else
  317. {
  318. if (DropDownShow && e.Node.Level == 0)
  319. {
  320. if (e.Node.Level == 0 && e.Node.Text == "")
  321. {
  322. this.Tag = null;
  323. this.Text = "";
  324. dropDown.Close();
  325. }
  326. else
  327. {
  328. this.StrGetName = "";
  329. this.Text = "";
  330. dropDown.Close();
  331. }
  332. }
  333. else
  334. {
  335. if (TreeView.SelectedNode.Name.ToString().Trim() != "")
  336. {
  337. this.StrGetName = TreeView.SelectedNode.Name.ToString();
  338. this.Tag = TreeView.SelectedNode.Tag;
  339. this.Text = TreeView.SelectedNode.Text.ToString();
  340. TreeView.SelectedNode.ForeColor = Color.Red;
  341. dropDown.Close();
  342. }
  343. else
  344. {
  345. if (TreeView.SelectedNode.Text.ToString().Trim() == "")
  346. {
  347. this.StrGetName = TreeView.SelectedNode.Name.ToString();
  348. this.Tag = TreeView.SelectedNode.Tag;
  349. this.Text = TreeView.SelectedNode.Text.ToString();
  350. TreeView.SelectedNode.ForeColor = Color.Red;
  351. dropDown.Close();
  352. }
  353. else
  354. {
  355. return;
  356. }
  357. }
  358. }
  359. }
  360. }
  361. if (this.ComboBoxTreeViewEx_TextChanged != null)
  362. ComboBoxTreeViewEx_TextChanged(sender, e);
  363. //选择后,关闭所有展开的节点
  364. /* foreach (TreeNode item in ((TreeView)sender).Nodes)
  365. {
  366. if (e.Node != item)
  367. {
  368. item.Collapse();
  369. }
  370. }*/
  371. }
  372. /// <summary>
  373. /// 点击一个节点展开,关闭其他展开的节点
  374. /// </summary>
  375. /// <param name="sender"></param>
  376. /// <param name="e"></param>
  377. private void treeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
  378. {
  379. if (isTextsearch)
  380. {
  381. return;
  382. }
  383. foreach (TreeNode item in ((TreeView)sender).Nodes)
  384. {
  385. if (e.Node != item && !item.Nodes.Contains(e.Node))
  386. {
  387. item.Collapse();
  388. }
  389. }
  390. }
  391. #endregion
  392. #region 根据Tag获取当前所指的Text
  393. /// <summary>
  394. /// 遍历树,根据Tag获取当前所指的Text
  395. /// </summary>
  396. /// <param name="sender"></param>
  397. /// <param name="e"></param>
  398. public void TagFindText(object ObjKey)
  399. {
  400. IsTagReturn = false;
  401. NodeClearBackColor();
  402. foreach (TreeNode node in this.TreeView.Nodes)
  403. {
  404. if (node.Tag != null)
  405. {
  406. if (node.Tag.Equals(ObjKey))
  407. {
  408. this.Text = node.Text;
  409. this.Tag = node.Tag;
  410. this.StrGetName = node.Name;
  411. //node.BackColor = System.Drawing.Color.FromArgb(224, 224, 224); //Color.LightPink;
  412. this.TreeView.SelectedNode = node;
  413. node.ForeColor = Color.Red;
  414. IsTagReturn = true;
  415. }
  416. else
  417. { TagForeachTreeNode(node, ObjKey); }
  418. }
  419. else
  420. { TagForeachTreeNode(node, ObjKey); }
  421. if (IsTagReturn)
  422. { return; }
  423. }
  424. }
  425. private bool IsTagReturn = false;
  426. /// <summary>
  427. /// 循环遍历子节点
  428. /// </summary>
  429. private void TagForeachTreeNode(TreeNode node, object Key)
  430. {
  431. foreach (TreeNode nde in node.Nodes)
  432. {
  433. if (nde.Tag.Equals(Key))
  434. {
  435. this.Text = nde.Text;
  436. this.Tag = nde.Tag;
  437. this.StrGetName = nde.Name;
  438. //nde.BackColor = System.Drawing.Color.FromArgb(224, 224, 224); //Color.Silver;
  439. this.TreeView.SelectedNode = nde;
  440. nde.ForeColor = Color.Red;
  441. node.ExpandAll();
  442. IsTagReturn = true;
  443. return;
  444. }
  445. else
  446. {
  447. if (nde.Nodes.Count > 0)
  448. {
  449. TagForeachTreeNode(nde, Key);
  450. }
  451. }
  452. }
  453. }
  454. #endregion
  455. #region 根据Text获取当前所指的Tag
  456. /// <summary>
  457. /// 遍历树,根据Text获取当前所指的Tag
  458. /// </summary>
  459. /// <param name="ObjText"></param>
  460. public void TextFindTag(string ObjText)
  461. {
  462. if (ObjText == null)
  463. {
  464. ObjText = "";
  465. }
  466. IsTextReturn = false;
  467. NodeClearBackColor();
  468. foreach (TreeNode node in TreeView.Nodes)
  469. {
  470. if (node.Tag != null)
  471. {
  472. if (node.Text.Equals(ObjText))
  473. {
  474. this.Text = node.Text;
  475. this.Tag = node.Tag;
  476. this.StrGetName = node.Name;
  477. this.TreeView.SelectedNode = node;
  478. node.ForeColor = Color.Red;
  479. IsTextReturn = true;
  480. }
  481. else
  482. { TextForeachTreeNode(node, ObjText); }
  483. }
  484. else
  485. { TextForeachTreeNode(node, ObjText); }
  486. if (IsTextReturn)
  487. { return; }
  488. }
  489. this.Text = ObjText.Trim();
  490. }
  491. private bool IsTextReturn = false;
  492. /// <summary>
  493. /// 循环遍历子节点
  494. /// </summary>
  495. private void TextForeachTreeNode(TreeNode node, string ObjText)
  496. {
  497. foreach (TreeNode nde in node.Nodes)
  498. {
  499. if (nde.Text.Equals(ObjText))
  500. {
  501. this.Text = nde.Text;
  502. this.Tag = nde.Tag;
  503. this.StrGetName = nde.Name;
  504. this.TreeView.SelectedNode = node;
  505. nde.ForeColor = Color.Red;
  506. node.Expand();
  507. if (node.Level > 0)
  508. { node.Parent.Expand(); }
  509. IsTextReturn = true;
  510. return;
  511. }
  512. else
  513. {
  514. if (nde.Nodes.Count > 0)
  515. { TextForeachTreeNode(nde, ObjText); }
  516. }
  517. }
  518. }
  519. /// <summary>
  520. /// 遍历树,根据Text获取当前所指的Tag
  521. /// </summary>
  522. /// <param name="ObjText"></param>
  523. public string TextFindParentText(string ObjText)
  524. {
  525. bool Isbl = false;
  526. string StrText = "";
  527. foreach (TreeNode node in TreeView.Nodes)
  528. {
  529. if (node.Tag != null)
  530. {
  531. if (node.Text.Equals(ObjText))
  532. {
  533. StrText = node.Text; Isbl = true;
  534. }
  535. else
  536. { StrText = TextForeachTreeNodeParentText(node, ObjText, ref Isbl); }
  537. }
  538. else
  539. { StrText = TextForeachTreeNodeParentText(node, ObjText, ref Isbl); }
  540. if (Isbl)
  541. { break; }
  542. }
  543. return StrText;
  544. }
  545. /// <summary>
  546. /// 循环遍历子节点
  547. /// </summary>
  548. private string TextForeachTreeNodeParentText(TreeNode node, string ObjText, ref bool Isbl)
  549. {
  550. string strText = "";
  551. foreach (TreeNode nde in node.Nodes)
  552. {
  553. if (nde.Text.Equals(ObjText))
  554. {
  555. strText = nde.Parent.Text;
  556. Isbl = true;
  557. }
  558. else
  559. {
  560. if (nde.Nodes.Count > 0)
  561. { strText = TextForeachTreeNodeParentText(nde, ObjText, ref Isbl); }
  562. }
  563. if (Isbl)
  564. { break; }
  565. }
  566. return strText;
  567. }
  568. #endregion
  569. #region 获取当前选择的最前的父节点
  570. /// <summary>
  571. /// 遍历树,根据Text获取当前所指的Tag
  572. /// </summary>
  573. /// <param name="ObjText"></param>
  574. public Hashtable TextFindParentIsParent(string ObjText)
  575. {
  576. bool Isbl = false;
  577. Hashtable htData = new Hashtable();
  578. if (ObjText != "")
  579. {
  580. foreach (TreeNode node in TreeView.Nodes)
  581. {
  582. if (node.Tag != null)
  583. {
  584. if (node.Text.Equals(ObjText))
  585. {
  586. htData = GetNodeParent(node);
  587. Isbl = true;
  588. }
  589. else
  590. { htData = TextForeachTreeNodeParentIsParent(node, ObjText, ref Isbl); }
  591. }
  592. else
  593. { htData = TextForeachTreeNodeParentIsParent(node, ObjText, ref Isbl); }
  594. if (Isbl)
  595. { break; }
  596. }
  597. }
  598. if (htData.Count <= 0)
  599. {
  600. htData["Text"] = "";
  601. htData["Tag"] = "";
  602. htData["Name"] = "";
  603. }
  604. return htData;
  605. }
  606. /// <summary>
  607. /// 循环遍历子节点
  608. /// </summary>
  609. private Hashtable TextForeachTreeNodeParentIsParent(TreeNode node, string ObjText, ref bool Isbl)
  610. {
  611. Hashtable htData = new Hashtable();
  612. foreach (TreeNode nde in node.Nodes)
  613. {
  614. if (nde.Text.Equals(ObjText))
  615. {
  616. htData = GetNodeParent(node);
  617. Isbl = true;
  618. }
  619. else
  620. {
  621. if (nde.Nodes.Count > 0)
  622. { htData = TextForeachTreeNodeParentIsParent(nde, ObjText, ref Isbl); }
  623. }
  624. if (Isbl)
  625. { break; }
  626. }
  627. return htData;
  628. }
  629. /// <summary>
  630. /// 获取当前选择的最前的父节点
  631. /// </summary>
  632. /// <param name="node"></param>
  633. /// <returns></returns>
  634. private Hashtable GetNodeParent(TreeNode node)
  635. {
  636. Hashtable htData = new Hashtable();
  637. if (node != null)
  638. {
  639. bool Isbl = true;
  640. while (Isbl)
  641. {
  642. if (node.Parent != null)
  643. {
  644. node = node.Parent;
  645. }
  646. else
  647. {
  648. htData["Text"] = node.Text.Trim();
  649. htData["Tag"] = node.Tag.ToString().Trim();
  650. htData["Name"] = node.Name.Trim();
  651. Isbl = false;
  652. }
  653. }
  654. }
  655. return htData;
  656. }
  657. #endregion
  658. #region 获取当前选择的最前的父节点
  659. /// <summary>
  660. /// 遍历树,根据Text获取当前所指的Tag
  661. /// </summary>
  662. /// <param name="ObjText"></param>
  663. public Hashtable TagFindParentIsParent(string ObjTag)
  664. {
  665. bool Isbl = false;
  666. Hashtable htData = new Hashtable();
  667. if (ObjTag != "")
  668. {
  669. foreach (TreeNode node in TreeView.Nodes)
  670. {
  671. if (node.Tag != null)
  672. {
  673. if (node.ToString().Trim().Equals(ObjTag))
  674. {
  675. htData = TagGetNodeParent(node);
  676. Isbl = true;
  677. }
  678. else
  679. { htData = TagForeachTreeNodeParentIsParent(node, ObjTag, ref Isbl); }
  680. }
  681. else
  682. { htData = TagForeachTreeNodeParentIsParent(node, ObjTag, ref Isbl); }
  683. if (Isbl)
  684. { break; }
  685. }
  686. }
  687. if (htData.Count <= 0)
  688. {
  689. htData["Text"] = "";
  690. htData["Tag"] = "";
  691. htData["Name"] = "";
  692. }
  693. return htData;
  694. }
  695. /// <summary>
  696. /// 循环遍历子节点
  697. /// </summary>
  698. private Hashtable TagForeachTreeNodeParentIsParent(TreeNode node, string ObjTag, ref bool Isbl)
  699. {
  700. Hashtable htData = new Hashtable();
  701. foreach (TreeNode nde in node.Nodes)
  702. {
  703. if (nde.Tag.ToString().Trim().Equals(ObjTag))
  704. {
  705. htData = TagGetNodeParent(node);
  706. Isbl = true;
  707. }
  708. else
  709. {
  710. if (nde.Nodes.Count > 0)
  711. { htData = TagForeachTreeNodeParentIsParent(nde, ObjTag, ref Isbl); }
  712. }
  713. if (Isbl)
  714. { break; }
  715. }
  716. return htData;
  717. }
  718. /// <summary>
  719. /// 获取当前选择的最前的父节点
  720. /// </summary>
  721. /// <param name="node"></param>
  722. /// <returns></returns>
  723. private Hashtable TagGetNodeParent(TreeNode node)
  724. {
  725. Hashtable htData = new Hashtable();
  726. if (node != null)
  727. {
  728. bool Isbl = true;
  729. while (Isbl)
  730. {
  731. if (node.Parent != null)
  732. {
  733. node = node.Parent;
  734. }
  735. else
  736. {
  737. htData["Text"] = node.Text.Trim();
  738. htData["Tag"] = node.Tag.ToString().Trim();
  739. htData["Name"] = node.Name.Trim();
  740. Isbl = false;
  741. }
  742. }
  743. }
  744. return htData;
  745. }
  746. #endregion
  747. /// <summary>
  748. /// 清除之前所选中的项的背景颜色
  749. /// </summary>
  750. /// <param name="node"></param>
  751. private void NodeClearBackColor()
  752. {
  753. foreach (TreeNode node in TreeView.Nodes)
  754. {
  755. //node.BackColor = Color.White;
  756. node.ForeColor = Color.Black;
  757. if (node.Nodes.Count > 0)
  758. { NodeClearBackColor(node); }
  759. }
  760. }
  761. /// <summary>
  762. /// 清除节点下的所有节点的背景颜色
  763. /// </summary>
  764. /// <param name="node"></param>
  765. private void NodeClearBackColor(TreeNode node)
  766. {
  767. foreach (TreeNode nde in node.Nodes)
  768. {
  769. //nde.BackColor = Color.White;
  770. nde.ForeColor = Color.Black;
  771. if (nde.Nodes.Count > 0)
  772. {
  773. NodeClearBackColor(nde);
  774. if (nde.Nodes.Count > 0)
  775. { NodeClearBackColor(nde); }
  776. }
  777. }
  778. }
  779. int _Nodelevel = 0;
  780. /// <summary>
  781. /// 无效属性
  782. /// </summary>
  783. [Obsolete("此属性过期不建议使用")]
  784. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
  785. public int Nodelevel
  786. {
  787. get { return _Nodelevel; }
  788. set { _Nodelevel = value; }
  789. }
  790. public TreeNode GetNodeItem_TextToTag(string ObjText)
  791. {
  792. foreach (TreeNode node in TreeView.Nodes)
  793. {
  794. if (node.Tag != null)
  795. {
  796. if (node.Text.Equals(ObjText))
  797. {
  798. return node;
  799. }
  800. else if (node.Nodes.Count > 0)
  801. {
  802. foreach (TreeNode node2 in node.Nodes)
  803. {
  804. if (node2.Tag != null)
  805. {
  806. if (node2.Text.Equals(ObjText))
  807. { return node2; }
  808. else if (node2.Nodes.Count > 0)
  809. {
  810. foreach (TreeNode node3 in node2.Nodes)
  811. {
  812. if (node3.Tag != null)
  813. {
  814. if (node3.Text.Equals(ObjText))
  815. { return node3; }
  816. }
  817. }
  818. }
  819. }
  820. }
  821. }
  822. }
  823. }
  824. return null;
  825. }
  826. public TreeNode GetNodeItem_TagToText(string ObjTag)
  827. {
  828. foreach (TreeNode node in TreeView.Nodes)
  829. {
  830. if (node.Tag != null)
  831. {
  832. if (node.Tag.Equals(ObjTag))
  833. {
  834. return node;
  835. }
  836. else if (node.Nodes.Count > 0)
  837. {
  838. foreach (TreeNode node2 in node.Nodes)
  839. {
  840. if (node2.Tag != null)
  841. {
  842. if (node2.Tag.Equals(ObjTag))
  843. { return node2; }
  844. else if (node2.Nodes.Count > 0)
  845. {
  846. foreach (TreeNode node3 in node2.Nodes)
  847. {
  848. if (node3.Tag != null)
  849. {
  850. if (node3.Tag.Equals(ObjTag))
  851. { return node3; }
  852. }
  853. }
  854. }
  855. }
  856. }
  857. }
  858. }
  859. }
  860. return null;
  861. }
  862. public string GetSelectedNodeName()
  863. {
  864. if (this.TreeView.SelectedNode != null)
  865. {
  866. return this.TreeView.SelectedNode.Name;
  867. }
  868. else {
  869. return "";
  870. }
  871. }
  872. public string GetSelectedNodeText()
  873. {
  874. if (this.TreeView.SelectedNode != null)
  875. {
  876. return this.TreeView.SelectedNode.Text;
  877. }
  878. else
  879. {
  880. return "";
  881. }
  882. }
  883. /// <summary>
  884. /// 设置选择项
  885. /// </summary>
  886. /// <param name="selectValue">要选中的值</param>
  887. /// <param name="isDisabled">是否禁用</param>
  888. public void SetNodeNameToSelectItme(string selectValue, bool isDisabled = false)
  889. {
  890. if (string.IsNullOrEmpty(selectValue))
  891. {
  892. this.TreeView.SelectedNode = null;
  893. this.Text = "";
  894. }
  895. else
  896. {
  897. foreach (TreeNode node in this.TreeView.Nodes)
  898. {
  899. if (node.Name.ToLower() == selectValue.ToLower())
  900. {
  901. this.TreeView.SelectedNode = node;
  902. this.Text = node.Text;
  903. break;
  904. }
  905. else if (node.Nodes.Count > 0)
  906. {
  907. NodeNameToSelectSubItem(node, selectValue);
  908. }
  909. }
  910. }
  911. if (isDisabled)
  912. {
  913. this.Enabled = false;
  914. }
  915. else
  916. {
  917. this.Enabled = true;
  918. }
  919. }
  920. public void NodeNameToSelectSubItem(TreeNode node, string selectValue)
  921. {
  922. foreach (TreeNode subNode in node.Nodes)
  923. {
  924. if (subNode.Name.ToLower() == selectValue.ToLower())
  925. {
  926. this.TreeView.SelectedNode = subNode;
  927. this.SelectedText = subNode.Text;
  928. break;
  929. }
  930. else if (subNode.Nodes.Count > 0)
  931. {
  932. NodeNameToSelectSubItem( subNode, selectValue);
  933. }
  934. }
  935. }
  936. /// <summary>
  937. /// 设置选择项
  938. /// </summary>
  939. /// <param name="cmbProject"></param>
  940. /// <param name="selectValue">要选中的值</param>
  941. /// <param name="isDisabled">是否禁用</param>
  942. public void SetNodeTextToSelectItme(string selectValue, bool isDisabled = false)
  943. {
  944. if (string.IsNullOrEmpty(selectValue))
  945. {
  946. this.TreeView.SelectedNode = null;
  947. this.Text = "";
  948. }
  949. else
  950. {
  951. foreach (TreeNode node in this.TreeView.Nodes)
  952. {
  953. if (node.Text.ToLower() == selectValue.ToLower())
  954. {
  955. this.TreeView.SelectedNode = node;
  956. this.Text = node.Text;
  957. break;
  958. }
  959. else if (node.Nodes.Count > 0)
  960. {
  961. NodeTextToSelectSubItem(node, selectValue);
  962. }
  963. }
  964. }
  965. if (isDisabled)
  966. {
  967. this.Enabled = false;
  968. }
  969. else
  970. {
  971. this.Enabled = true;
  972. }
  973. }
  974. public void NodeTextToSelectSubItem(TreeNode node, string selectValue)
  975. {
  976. foreach (TreeNode subNode in node.Nodes)
  977. {
  978. if (subNode.Text.ToLower() == selectValue.ToLower())
  979. {
  980. this.TreeView.SelectedNode = subNode;
  981. this.SelectedText = subNode.Text;
  982. break;
  983. }
  984. else if (subNode.Nodes.Count > 0)
  985. {
  986. NodeTextToSelectSubItem( subNode, selectValue);
  987. }
  988. }
  989. }
  990. }
  991. }