在combobox中嵌入treeview

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值