ComboBox与TreeView组合控件 ComboBoxTree(转帖)

平时常用无限级分类,只是这个无限级树,每次都得拖ComboBox,设置,拖TreeView,再设置,代码里也得整来整去,很是麻烦,也很是不爽,所以改造下。

不过我的水平想组合这两个东西,可太有难度了,所以还是上网上找到,在CodeProject上找到一个评价还不错的,拿下来修改,改造了两天,终于可以很方便的使用了。

不过还是我的水平问题,这个控件使用的东西很多理解不了,所以感觉改的不是很好,只能说能很方便的使用。

调用方法:

填充:comboBoxTree1.Fill(DataTable dt, object ParentID)

        ///


        /// 填充Tree
        ///

        /// 父级编号
        /// 格式,三列,Id,Name,ParentId,只要顺序一样就可以了
        public void Fill(DataTable dt, object ParentID)

设置值:SelectedByValue(object value, object text)

        ///


        /// 根据值来查找
        ///

        ///
        /// Text可以随便写,当然,最好是什么就写什么
        public void SelectedByValue(object value, object text)

取值:Text,Value,FullPath

comboBoxTree1.Value
comboBoxTree1.Text
comboBoxTree1.FullPath

主要代码太多,这里只发他的调用方法吧:

view plaincopy to clipboardprint?

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Data.SqlClient;  
  6. using System.Drawing;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. namespace Demo  
  10. {  
  11. public partial class Form1 : Form  
  12.     {  
  13. public Form1()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17. private void Form1_Load(object sender, EventArgs e)  
  18.         {  
  19.             comboBoxTree1.Fill(GetDataTable(), "0");  
  20.         }  
  21. private void btnGet_Click(object sender, EventArgs e)  
  22.         {  
  23.             MessageBox.Show(  
  24. "Value:"+comboBoxTree1.Value+  
  25. "\nText:"+comboBoxTree1.Text+  
  26. "\nFullPath:"+comboBoxTree1.FullPath  
  27.                 );  
  28.         }  
  29. private void btnSet_Click(object sender, EventArgs e)  
  30.         {  
  31. //comboBoxTree1.SelectedByValue(3, "Name333");
  32.             comboBoxTree1.SelectedByValue(3, "Name3");  
  33.         }  
  34. private DataTable GetDataTable()  
  35.         {  
  36. string[,] array3D = {   
  37.                                 { "1", "ComboBoxTree1", "0"},   
  38.                                 { "2", "ComboBoxTree2", "1"},   
  39.                                 { "3", "ComboBoxTree3", "1"},   
  40.                                 { "4", "ComboBoxTree4", "2"},   
  41.                                 { "5", "ComboBoxTree5", "2"},   
  42.                                 { "6", "ComboBoxTree6", "5"},   
  43.                                 };  
  44. return ArrayToDataTable.ArrayToDataTable.Convert(array3D);  
  45.         }  
  46. private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)  
  47.         {  
  48.             System.Diagnostics.Process.Start("Iexplore.exe", "http://www.yongfa365.com/");  
  49.         }  
  50.     }  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBoxTree1.Fill(GetDataTable(), "0");
        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            MessageBox.Show(
                "Value:"+comboBoxTree1.Value+
                "\nText:"+comboBoxTree1.Text+
                "\nFullPath:"+comboBoxTree1.FullPath
                
                );
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            //comboBoxTree1.SelectedByValue(3, "Name333");
            comboBoxTree1.SelectedByValue(3, "Name3");
        }



        private DataTable GetDataTable()
        {

            string[,] array3D = { 
                                { "1", "ComboBoxTree1", "0"}, 
                                { "2", "ComboBoxTree2", "1"}, 
                                { "3", "ComboBoxTree3", "1"}, 
                                { "4", "ComboBoxTree4", "2"}, 
                                { "5", "ComboBoxTree5", "2"}, 
                                { "6", "ComboBoxTree6", "5"}, 
                                };
            return ArrayToDataTable.ArrayToDataTable.Convert(array3D);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("Iexplore.exe", "http://www.yongfa365.com/");
        }

    }
}

演示文件及相关源码下载地址:ComboBoxTree.rar

主要代码来源与:http://www.codeproject.com/KB/miscctrl/CSDropDownPanel.aspx

posted on 2010-06-05 17:21  guoxuefeng  阅读(1952)  评论(1编辑  收藏  举报