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?
- 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/");
- }
- }
- }
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
浙公网安备 33010602011771号