关闭

在combobox中嵌入treeview

标签: dropdownobjectnullclass2010
2854人阅读 评论(1) 收藏 举报
分类:

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
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:189728次
    • 积分:2541
    • 等级:
    • 排名:第14457名
    • 原创:55篇
    • 转载:80篇
    • 译文:0篇
    • 评论:10条
    最新评论