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);
}
}
}
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);
}
}
}