贫民窟里的程序高手

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

首先,要新建一个组件。

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace BaseControls
{
    public partial class DMComboTree : ComboBox
    {
        private bool m_blDropShow = false;
        private bool m_blPopupAutoSize = false;
        private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;

        private ToolStripControlHost treeHost;
        private ToolStripDropDown dropDown;
        TreeView vTreeView = new TreeView();
        public DMComboTree()
        {
            //InitializeComponent();
        }

        public DMComboTree(IContainer container)
        {
            container.Add(this);
             
            InitializeComponent();
        }

        void DrawTree() 
        {
            vTreeView.Scrollable = true;
            //下拉框的大小需要调整树的大小来调整。
            vTreeView.Height = 200;
            vTreeView.NodeMouseDoubleClick -= new TreeNodeMouseClickEventHandler(TreeView_NodeMouseDoubleClick);
            vTreeView.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(TreeView_NodeMouseDoubleClick);

            treeHost = new ToolStripControlHost(vTreeView);
            treeHost.AutoSize = true;

            dropDown = new ToolStripDropDown();
            dropDown.Items.Add(treeHost);
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);

            if (this.Width - e.X < 20) 
            {
                DrawTree();
                dropDown.Show(this, 0, this.Height);
                vTreeView.ExpandAll();
                dropDown.Focus();
            }
        }

        /// <summary>
        /// 树形双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void TreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            base.Text = e.Node.Text;
            TreeValue = e.Node.Name;
            dropDown.Close();
        }

        string _treeValue = null;

        public string TreeValue
        {
            get { return _treeValue; }
            set 
            {
                _treeValue = value;
                if (TreeValueChanged != null) 
                {
                    if (TreeValueChanged != null) 
                    {
                        TreeValueChanged(this, new EventArgs());
                    }
                }
            }
        }

        //protected override void WndProc(ref Message m)
        //{
        //    if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
        //    {
        //        if (m_blDropShow)
        //        {
        //            m_blDropShow = false;
        //        }
        //        else
        //        {
        //            m_blDropShow = true;
        //        }
        //        if (m_blDropShow)
        //        {
        //            DrawTree();
        //            dropDown.Show(this, 0, this.Height);
        //        }
        //        else
        //        {
        //            dropDown.Close();
        //        }
        //        return;
        //    }
        //    base.WndProc(ref m);
        //}

        public void BindTree(DataTable vDt) 
        {
            var q = from b in vDt.AsEnumerable() where b.Field<Int32>("Parent") == 0 select b;

            if (q != null && q.Count<DataRow>() > 0)
            {
                DataTable dt = q.CopyToDataTable<DataRow>();
                q = null;
                foreach (DataRow dr in dt.Rows)
                {
                    TreeNode n = new TreeNode();
                    n.Name = dr["Id"].ToString();
                    n.Text = dr["Name"].ToString();
                    BindTree(n, vDt);
                    vTreeView.Nodes.Add(n);
                }

                dt.Dispose();
            }
            
            vDt.Dispose();

            //设置一个全部品牌用来取消选择项
            TreeNode nullNode = new TreeNode();
            nullNode.Name = null;
            nullNode.Text = "全部品牌";
            vTreeView.Nodes.Insert(0, nullNode);
        }

        /// <summary>
        /// 递归绑定Node
        /// </summary>
        /// <param name="node"></param>
        /// <param name="vDt"></param>
        void BindTree(TreeNode node, DataTable vDt) 
        {
            var q = from b in vDt.AsEnumerable() where b.Field<Int32>("Parent") == int.Parse(node.Name) select b;

            if (q != null && q.Count<DataRow>() > 0)
            {
                DataTable dt = q.CopyToDataTable<DataRow>();
                q = null;
                foreach (DataRow dr in dt.Rows)
                {
                    TreeNode n = new TreeNode();
                    n.Name = dr["Id"].ToString();
                    n.Text = dr["Name"].ToString();
                    BindTree(n, vDt);
                    node.Nodes.Add(n);
                }

                dt.Dispose();
            }
        }

        public delegate void OnTreeValueChanged(object sender, EventArgs e);
        public event OnTreeValueChanged TreeValueChanged;
    }
}

 

posted on 2014-08-08 15:23  贫民窟里的程序高手  阅读(597)  评论(0编辑  收藏  举报