123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- 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;
- namespace LYFZ.ComponentLibrary
- {
- public partial class ComboBoxTreeViewEx : ComboBox
- {
- private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
- ToolStripControlHost treeViewHost;
- ToolStripDropDown dropDown;
- public ComboBoxTreeViewEx()
- {
- TreeView treeView = new TreeView();
- treeView.AfterSelect += new TreeViewEventHandler(treeView_AfterSelect);
- treeView.BorderStyle = BorderStyle.None;
- treeViewHost = new ToolStripControlHost(treeView);
- dropDown = new ToolStripDropDown();
- dropDown.Width = this.Width;
- dropDown.Items.Add(treeViewHost);
- }
- ///// <summary>
- /////
- ///// </summary>
- ///// <param name="sender"></param>
- ///// <param name="e"></param>
- //public void treeView_AfterSelect(object sender, TreeViewEventArgs e)
- //{
- // if (e.Node.Nodes.Count <= 0)//判断选中节点是否有子节点
- // {
- // this.Text = TreeView.SelectedNode.Text;
- // dropDown.Close();
- // }
- //}
- 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; }
- }
- /// <summary>
- /// 节点选择事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
- {
- if (e.Node.Nodes.Count <= 0)//判断选中节点是否有子节点
- {
- if (DropDownShow && e.Node.Level == 0)
- {
- this.Name = "";
- this.Text = "";
- dropDown.Show();
- }
- else
- {
- this.Name = TreeView.SelectedNode.Name.ToString();
- if (Name != "")
- {
- this.Tag = TreeView.SelectedNode.Tag.ToString();
- this.Text = TreeView.SelectedNode.Text.ToString();
- dropDown.Close();
- }
- }
- }
- if (this.ComboBoxTreeViewEx_TextChanged != null)
- ComboBoxTreeViewEx_TextChanged(sender, e);
- }
- #endregion
- }
- }
|