在combobox中嵌入treeview

本文介绍了一种将TreeView控件嵌套在ComboBox中的方法,实现了用户可以选择树状结构中的节点作为组合框项的功能。通过自定义类ComboBoxTreeView,利用Windows Forms控件实现了一个可以展开并选择树节点的下拉组合框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值