ComboBoxTreeViewEx.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. namespace LYFZ.ComponentLibrary
  10. {
  11. public partial class ComboBoxTreeViewEx : ComboBox
  12. {
  13. private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
  14. ToolStripControlHost treeViewHost;
  15. ToolStripDropDown dropDown;
  16. public ComboBoxTreeViewEx()
  17. {
  18. TreeView treeView = new TreeView();
  19. treeView.AfterSelect += new TreeViewEventHandler(treeView_AfterSelect);
  20. treeView.BorderStyle = BorderStyle.None;
  21. treeViewHost = new ToolStripControlHost(treeView);
  22. dropDown = new ToolStripDropDown();
  23. dropDown.Width = this.Width;
  24. dropDown.Items.Add(treeViewHost);
  25. }
  26. ///// <summary>
  27. /////
  28. ///// </summary>
  29. ///// <param name="sender"></param>
  30. ///// <param name="e"></param>
  31. //public void treeView_AfterSelect(object sender, TreeViewEventArgs e)
  32. //{
  33. // if (e.Node.Nodes.Count <= 0)//判断选中节点是否有子节点
  34. // {
  35. // this.Text = TreeView.SelectedNode.Text;
  36. // dropDown.Close();
  37. // }
  38. //}
  39. public TreeView TreeView
  40. {
  41. get { return treeViewHost.Control as TreeView; }
  42. }
  43. private void ShowDropDown()
  44. {
  45. if (dropDown != null)
  46. {
  47. treeViewHost.Size = new Size(DropDownWidth - 2, DropDownHeight);
  48. dropDown.Show(this, 0, this.Height);
  49. }
  50. }
  51. protected override void WndProc(ref Message m)
  52. {
  53. if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
  54. {
  55. ShowDropDown();
  56. return;
  57. }
  58. base.WndProc(ref m);
  59. }
  60. protected override void Dispose(bool disposing)
  61. {
  62. if (disposing)
  63. {
  64. if (dropDown != null)
  65. {
  66. dropDown.Dispose();
  67. dropDown = null;
  68. }
  69. }
  70. base.Dispose(disposing);
  71. }
  72. #region 定义文本框值更改后触发事件
  73. //定义delegate
  74. public delegate void ComboBoxTreeView_TextChanged(object sender, TreeViewEventArgs e);
  75. //用event 关键字声明事件对象
  76. /// <summary>
  77. ///
  78. /// </summary>
  79. [Category("控件扩展事件"), Description("文本框值修改时")]
  80. public event ComboBoxTreeView_TextChanged ComboBoxTreeViewEx_TextChanged;
  81. private bool _dropdownshow = false;
  82. /// <summary>
  83. /// 控件下拉是否展开还是隐藏
  84. /// </summary>
  85. public bool DropDownShow
  86. {
  87. get { return _dropdownshow; }
  88. set { _dropdownshow = value; }
  89. }
  90. /// <summary>
  91. /// 节点选择事件
  92. /// </summary>
  93. /// <param name="sender"></param>
  94. /// <param name="e"></param>
  95. private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
  96. {
  97. if (e.Node.Nodes.Count <= 0)//判断选中节点是否有子节点
  98. {
  99. if (DropDownShow && e.Node.Level == 0)
  100. {
  101. this.Name = "";
  102. this.Text = "";
  103. dropDown.Show();
  104. }
  105. else
  106. {
  107. this.Name = TreeView.SelectedNode.Name.ToString();
  108. if (Name != "")
  109. {
  110. this.Tag = TreeView.SelectedNode.Tag.ToString();
  111. this.Text = TreeView.SelectedNode.Text.ToString();
  112. dropDown.Close();
  113. }
  114. }
  115. }
  116. if (this.ComboBoxTreeViewEx_TextChanged != null)
  117. ComboBoxTreeViewEx_TextChanged(sender, e);
  118. }
  119. #endregion
  120. }
  121. }