winform没有默认的下拉tree的控件可是有些多级的列表需要下拉tree,下面我们的代码实现对Combbox进行扩展让它下拉显示tree.
- using System.Drawing;
- using System.Windows.Forms;
- namespace WindowsApplication23
- {
- 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);
- }
- }
- }