下拉列表框弹出树(ComboBoxTreeView)

来源:互联网 发布:windows store app 编辑:程序博客网 时间:2024/04/30 08:25
using System.Data;using System.Text;using System.Windows.Forms;namespace WinApp{    public class ComboBoxTreeView : ComboBox    {        private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;        ToolStripControlHost treeViewHost;        ToolStripDropDown dropDown;        public ComboBoxTreeView()        {            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);        }        public void treeView_AfterSelect(object sender, TreeViewEventArgs e)        {            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);        }    }}

原地址:http://blog.csdn.net/istarsoft/article/details/2773704
0 0