在combobox中嵌入treeview

原创 2011年01月11日 17:11:00

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ZhongBaoTech.LS.LSUI
{
    /// <summary>
    /// 嵌套了combobox的treeview
    /// 郑雯柯2010-12-29
    /// </summary>
    public class ComboBoxTreeView : ComboBox
    {
        private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
        ToolStripControlHost treeViewHost;
        ToolStripDropDown dropDown;

        public ComboBoxTreeView(TreeView treeView)
        {
            treeView.AfterSelect += new TreeViewEventHandler(treeView_AfterSelect);
            treeView.BorderStyle = BorderStyle.None;
            treeView.HideSelection = false;
            treeViewHost = new ToolStripControlHost(treeView);
            dropDown = new ToolStripDropDown();
            dropDown.Width = this.Width;
            dropDown.Items.Add(treeViewHost);
            this.KeyPress += new KeyPressEventHandler(MyKeyPress);
        }

        public void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.Items.Clear();
            this.Items.Add(TreeView.SelectedNode.Text);
            this.Text = TreeView.SelectedNode.Text;
            this.Name = TreeView.SelectedNode.Name;
            this.Tag = TreeView.SelectedNode.Tag;
            //this.Focus();
            dropDown.Close();
        }

        public void SetCurSelected(TreeNode node)
        {
            this.TreeView.SelectedNode = node;
            this.Items.Clear();
            this.Items.Add(TreeView.SelectedNode.Text);
            this.SelectedIndex = 0;
            this.Name = TreeView.SelectedNode.Name;
            this.Tag = TreeView.SelectedNode.Tag;
        }

        public TreeView TreeView
        {
            get { return treeViewHost.Control as TreeView; }
        }

        private void MyKeyPress(object sender, KeyPressEventArgs e)
        {
            e.KeyChar = (char)0;
        }

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

 

调用:

            ComboBoxTreeView comboTrv;  //嵌套了treeview的combobox

            TreeView trv = new TreeView();
            ClsTreeView.InitMateCodeStruList(trv, TC_MateCodeStruList.List(LSUserInfo.Pid), true);
            trv.Nodes[0].Expand();
            comboTrv = new ComboBoxTreeView(trv);
            comboTrv.SelectedIndexChanged += comboBox6_SelectedIndexChanged;
            comboTrv.Left = 92;
            comboTrv.Top = 8;
            comboTrv.Width = 221;
            this.panel2.Controls.Add(comboTrv);

相关文章推荐

Winform中如何实现下拉树效果(类似于ComboBox下拉时显示的是树状结构)

Winform中如何实现下拉树效果 简介:Winform中如何实现下拉树效果(类似于ComboBox下拉时显示的是树状结构),         如果是BS的WebForm就有很多解决方案了,Devex...

ComboTreeView控件-Winform树型ComboBox

控件源码:using System.Data;using System.Text;using System.Windows.Forms; public class ComboBoxTreeVie...
  • CutBug
  • CutBug
  • 2007年10月18日 12:02
  • 5751

下拉列表框弹出树(ComboBoxTreeView)

 using System.Data;using System.Text;using System.Windows.Forms;namespace WinApp{    public class Co...

WPF 制作带TreeView的ComBox

1、xaml代码部分

WPF 自定义TreeViewItem样式

首先自定义样式的目的是觉得

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

自定义控件---动态无限级生成 级联comboBox 和 treeView

开始 -〉新建 -〉新建项目 -〉“Windows窗体控件库” 名称栏 里 改为: myControl1 贴上下面的代码: using System; using System.Collectio...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:在combobox中嵌入treeview
举报原因:
原因补充:

(最多只允许输入30个字)