这个控件是VS.net自带的,不用下载给我email,我发给你例子
解决方案 »
- 请教OPenFileDialog类的InitialDirectory属性中路径保持和恢复的问题
- XML树存入数据库的问题
- 如何在窗体之间传值?在线等.......
- 还是datagrid的简单问题
- 请问怎么用C#获取局域网内的所有机器名和IP地址
- 用C#设计一个学生毕业设计管理程序
- 这个正则表达式错在哪里?
- c# foreach语句如何循环取arraylist对象中的类
- 服务器和客户端建立SOCKET连接之后,服务器怎么知道客户端的IP啊?
- 咨询一个关于C#Rdlc报表的问题
- 如何使用treeview,能不能把dataset数据绑定到treeview中,急急急急急急急!!!!!!在线等待!
- 请教如何通过Socket向Smtp服务器发送验证请求?
using System.IO;
using System.Resources;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms; // <doc>
// <desc>
// 此示例演示如何使用 Treeview 控件
// </desc>
// </doc>
//
public class TreeViewCtl : System.Windows.Forms.Form { public TreeViewCtl() {
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); FillDirectoryTree();
imageListComboBox.SelectedIndex = 1;
indentUpDown.Value = directoryTree.Indent;
} /// <summary>
/// 清理正在使用的所有资源
/// </summary>
public new void Dispose() {
base.Dispose();
components.Dispose();
} // <doc>
// <desc>
// 对于给定的根目录(或驱动器),向 directoryTree
// 添加目录。
// </desc>
// </doc>
//
private void AddDirectories(TreeNode node) {
try {
DirectoryInfo dir = new DirectoryInfo(GetPathFromNode(node));
DirectoryInfo[] e = dir.GetDirectories(); for (int i = 0; i < e.Length; i++) {
string name = e[i].Name;
if (!name.Equals(".") && !name.Equals("..")) {
node.Nodes.Add(new DirectoryNode(name));
}
}
}
catch (Exception e) {
MessageBox.Show(e.Message);
}
} // <doc>
// <desc>
// 对于给定的节点,在 directoryTree 中为节点的子级
// 添加子目录。
// </desc>
// </doc>
//
private void AddSubDirectories(DirectoryNode node) {
for (int i = 0; i < node.Nodes.Count; i++) {
AddDirectories(node.Nodes[i]);
}
node.SubDirectoriesAdded = true;
} // <doc>
// <desc>
// directoryTree 上 afterSelect 事件的事件处理程序。
// 更改标题栏以显示选定 directoryNode 的路径。
// </desc>
// </doc>
//
private void directoryTree_AfterSelect(object source, TreeViewEventArgs e) {
Text = "Windows.Forms 文件资源管理器 - " + e.Node.Text;
} // <doc>
// <desc>
// directoryTree 上 beforeExpand 事件的事件处理程序。
// 如果尚未展开该节点,请展开它。
// </desc>
// </doc>
//
private void directoryTree_BeforeExpand(object source, TreeViewCancelEventArgs e) {
DirectoryNode nodeExpanding = (DirectoryNode)e.Node;
if (!nodeExpanding.SubDirectoriesAdded)
AddSubDirectories(nodeExpanding);
} // <doc>
// <desc>
// 用于在创建 TreeViewCtl 窗体时初始化 directoryTree。
// </desc>
// </doc>
//
private void FillDirectoryTree() {
string[] drives = Environment.GetLogicalDrives();
for (int i = 0; i < drives.Length; i++) {
if (PlatformInvokeKernel32.GetDriveType(drives[i]) == PlatformInvokeKernel32.DRIVE_FIXED) {
DirectoryNode cRoot = new DirectoryNode(drives[i]);
directoryTree.Nodes.Add(cRoot);
AddDirectories(cRoot);
}
}
} // <doc>
// <desc>
// 返回节点的目录路径。
// </desc>
// <retvalue>
// 节点的目录路径。
// </retvalue>
// </doc>
//
private string GetPathFromNode(TreeNode node) {
if (node.Parent == null) {
return node.Text;
}
return Path.Combine(GetPathFromNode(node.Parent), node.Text);
} // <doc>
// <desc>
// 刷新帮助器函数以获取给定节点下的所有已展开的
// 节点。
// </desc>
// <param term='expandedNodes'>
// 对路径数组的引用,这些路径指向请求刷新时处于
// 扩展状态的所有节点。
// </param>
// <param term='startIndex'>
// ExpandedNodes 的数组索引,从此处开始将项添加到 ExpandedNodes。
// </param>
// <retvalue>
// 新的 StartIndex,即 StartIndex 的给定值 + 添加
// 到 ExpandedNodes 的项数。
// </retvalue>
// </doc>
//
private int Refresh_GetExpanded(TreeNode Node, string[] ExpandedNodes, int StartIndex) { if (StartIndex < ExpandedNodes.Length) {
if (Node.IsExpanded) {
ExpandedNodes[StartIndex] = Node.Text;
StartIndex++;
for (int i = 0; i < Node.Nodes.Count; i++) {
StartIndex = Refresh_GetExpanded(Node.Nodes[i],
ExpandedNodes,
StartIndex);
}
}
return StartIndex;
}
return -1;
} // <doc>
// <desc>
// 刷新帮助器函数以展开其路径在参数 ExpandedNodes 中的
// 所有节点。
// </desc>
// <param term='node'>
// 从其开始展开的节点。
// </param>
// <param term='expandedNodes'>
// 字符串数组,是要展开的所有节点的路径名。
// </param>
// </doc>
//
private void Refresh_Expand(TreeNode Node, string[] ExpandedNodes) { for (int i = ExpandedNodes.Length - 1; i >= 0; i--) {
if (ExpandedNodes[i] == Node.Text) {
/*
* 为了正确显示展开按钮,必须将一级
* 不可见子级添加到目录树。
*/
AddSubDirectories((DirectoryNode) Node);
Node.Expand(); /* 如果展开节点,则展开在刷新前
* 展开的所有子级。
*/
for (int j = 0; j < Node.Nodes.Count; j++) {
Refresh_Expand(Node.Nodes[j], ExpandedNodes);
} return;
}
}
} // <doc>
// <desc>
// 刷新视图,方法是删除所有节点然后通过读取磁盘
// 来还原它们。任何在 directoryView 中展开的
// 节点在刷新后都将展开。
// </desc>
// <param term='node'>
// - 从其开始刷新的节点。通常,这是根节点。
//
// </param>
// </doc>
//
private void Refresh(TreeNode node) {
// 更新 directoryTree
if (node.Nodes.Count > 0) {
if (node.IsExpanded) {
// 保存所有以 node 为根的展开节点,
// 包括那些间接以 node 为根的节点。
string[] tooBigExpandedNodes = new string[node.GetNodeCount(true)];
int iExpandedNodes = Refresh_GetExpanded(node,
tooBigExpandedNodes,
0);
string[] expandedNodes = new string[iExpandedNodes];
Array.Copy(tooBigExpandedNodes, 0, expandedNodes, 0,
iExpandedNodes); node.Nodes.Clear();
AddDirectories(node); // 因此具有子目录的子级显示为具有展开/折叠
// 按钮。
AddSubDirectories((DirectoryNode)node);
node.Expand(); /*
* 检查所有子级。一些节点可能从外部应用
if (node.Nodes[j].Nodes.Count > 0) {
// 如果该子节具有子目录。如果该子级在
// 刷新前是展开的,则在刷新后展开。
Refresh_Expand(node.Nodes[j], expandedNodes);
}
}
} else {
/*
* 如果该节点不是展开的,则不需要检查是否
* 展开了任何子级。但是,应通过读取驱动器
* 来更新目录树,以防外部应用程序添加/移除
* 了任何目录。
*/
node.Nodes.Clear();
AddDirectories(node);
}
} else {
/*
* 再有,如果没有任何子级,则没有必要考虑
* 展开的节点,但如果外部应用程序添加/移除
* 了任何目录,应将其反映出来。
*/
node.Nodes.Clear();
AddDirectories(node);
}
} private void checkBox1_Click(object source, EventArgs e) {
this.directoryTree.Sorted = checkBox1.Checked;
for (int i = 0; i < directoryTree.Nodes.Count; i++) {
Refresh(directoryTree.Nodes[i]);
}
} private void imageListComboBox_SelectedIndexChanged(object source, EventArgs e) {
int index = imageListComboBox.SelectedIndex;
if (index == 0) {
directoryTree.ImageList = null;
} else if (index == 1) {
directoryTree.ImageList = imageList1;
} else {
directoryTree.ImageList = imageList2;
}
} private void indentUpDown_ValueChanged(Object source, EventArgs e) {
directoryTree.Indent = (int)indentUpDown.Value;
} private void CheckBox2_click(object source, EventArgs e) {
this.directoryTree.HotTracking = checkBox2.Checked;
} private void CheckBox3_click(object source, EventArgs e) {
this.directoryTree.ShowLines = checkBox3.Checked;
} private void CheckBox4_click(object source, EventArgs e) {
this.directoryTree.ShowRootLines = checkBox4.Checked;
} private void CheckBox5_click(object source, EventArgs e) {
this.directoryTree.ShowPlusMinus = checkBox5.Checked;
} private void CheckBox6_click(object source, EventArgs e) {
this.directoryTree.CheckBoxes = checkBox6.Checked;
} private void CheckBox7_click(object source, EventArgs e) {
this.directoryTree.HideSelection = checkBox7.Checked;
} /// <summary>
/// 需要的设计器变量。
/// </summary>
private System.ComponentModel.Container components;
protected internal System.Windows.Forms.TreeView directoryTree;
protected internal System.Windows.Forms.ImageList imageList1;
protected internal System.Windows.Forms.ImageList imageList2;
protected internal System.Windows.Forms.GroupBox grpTreeView;
protected internal System.Windows.Forms.CheckBox checkBox1;
protected internal System.Windows.Forms.ComboBox imageListComboBox;
protected internal System.Windows.Forms.Label label1;
protected internal System.Windows.Forms.NumericUpDown indentUpDown;
protected internal System.Windows.Forms.Label label4;
protected internal System.Windows.Forms.CheckBox checkBox2;
protected internal System.Windows.Forms.CheckBox checkBox3;
protected internal System.Windows.Forms.CheckBox checkBox4;
protected internal System.Windows.Forms.CheckBox checkBox5;
protected internal System.Windows.Forms.CheckBox checkBox6;
protected internal System.Windows.Forms.CheckBox checkBox7;
protected internal System.Windows.Forms.ToolTip toolTip1; /// <summary>
/// 设计器支持所必需的方法,不要使用
/// 代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(TreeViewCtl)); this.components = new System.ComponentModel.Container();
this.checkBox7 = new System.Windows.Forms.CheckBox();
this.directoryTree = new System.Windows.Forms.TreeView();
this.checkBox5 = new System.Windows.Forms.CheckBox();
this.label4 = new System.Windows.Forms.Label();
this.indentUpDown = new System.Windows.Forms.NumericUpDown();
this.imageList2 = new System.Windows.Forms.ImageList();
this.toolTip1 = new System.Windows.Forms.ToolTip(components);
this.checkBox6 = new System.Windows.Forms.CheckBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.checkBox3 = new System.Windows.Forms.CheckBox();
this.imageList1 = new System.Windows.Forms.ImageList();
this.imageListComboBox = new System.Windows.Forms.ComboBox();
this.checkBox4 = new System.Windows.Forms.CheckBox();
this.grpTreeView = new System.Windows.Forms.GroupBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label(); checkBox7.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
checkBox7.Location = new System.Drawing.Point(16, 160);
checkBox7.TabIndex = 6;
checkBox7.CheckState = System.Windows.Forms.CheckState.Checked;
checkBox7.Text = "hideSelected";
checkBox7.Size = new System.Drawing.Size(100, 23);
checkBox7.Checked = true;
toolTip1.SetToolTip(checkBox7, "当控件不具有焦点时从选定的节点移除突出显示。");
checkBox7.Click += new EventHandler(CheckBox7_click); directoryTree.ImageList = (ImageList)imageList1;
directoryTree.ForeColor = (Color)System.Drawing.SystemColors.WindowText;
directoryTree.Location = new System.Drawing.Point(24, 16);
directoryTree.AllowDrop = true;
directoryTree.TabIndex = 0;
directoryTree.Indent = 19;
directoryTree.Text = "treeView1";
directoryTree.SelectedImageIndex = 1;
directoryTree.Size = new System.Drawing.Size(200, 264);
toolTip1.SetToolTip(directoryTree, "指示在同辈节点之间以及父节点与子节点之间是否显示连线");
directoryTree.BeforeExpand += new TreeViewCancelEventHandler(directoryTree_BeforeExpand); checkBox5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
checkBox5.Location = new System.Drawing.Point(16, 112);
checkBox5.TabIndex = 4;
checkBox5.CheckState = System.Windows.Forms.CheckState.Checked;
checkBox5.Text = "showPlusMinus";
checkBox5.Size = new System.Drawing.Size(120, 23);
checkBox5.Checked = true;
toolTip1.SetToolTip(checkBox5, "指示在父级旁边是否显示加号/减号按钮。");
checkBox5.Click += new EventHandler(CheckBox5_click); label4.Location = new System.Drawing.Point(16, 224);
label4.TabIndex = 9;
label4.TabStop = false;
label4.Text = "缩进:";
label4.Size = new System.Drawing.Size(48, 16); indentUpDown.Location = new System.Drawing.Point(88, 224);
indentUpDown.Maximum = new System.Decimal(150d);
indentUpDown.Minimum = new System.Decimal(18d);
indentUpDown.TabIndex = 10;
indentUpDown.Text = "18";
indentUpDown.DecimalPlaces = 0;
indentUpDown.Size = new System.Drawing.Size(120, 20);
toolTip1.SetToolTip(indentUpDown, "子节点的缩进宽度(以像素为单位)。");
indentUpDown.ValueChanged += new EventHandler(indentUpDown_ValueChanged); imageList2.ImageStream = (System.Windows.Forms.ImageListStreamer)resources.GetObject("imageList2.ImageStream");
imageList2.TransparentColor = (Color)System.Drawing.Color.Transparent; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(502, 293);
this.Text = "树视图"; toolTip1.Active = true; checkBox6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
checkBox6.Location = new System.Drawing.Point(16, 136);
checkBox6.TabIndex = 5;
checkBox6.Text = "checkBoxes";
checkBox6.Size = new System.Drawing.Size(100, 23);
toolTip1.SetToolTip(checkBox6, "指示在节点旁边是否显示复选框");
checkBox6.Click += new EventHandler(CheckBox6_click); checkBox1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
checkBox1.Location = new System.Drawing.Point(16, 16);
checkBox1.TabIndex = 0;
checkBox1.Text = "已排序";
checkBox1.Size = new System.Drawing.Size(100, 23);
toolTip1.SetToolTip(checkBox1, "指示是否对节点进行排序。");
checkBox1.Click += new EventHandler(checkBox1_Click); checkBox3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
checkBox3.Location = new System.Drawing.Point(16, 64);
checkBox3.TabIndex = 2;
checkBox3.CheckState = System.Windows.Forms.CheckState.Checked;
checkBox3.Text = "showLines";
checkBox3.Size = new System.Drawing.Size(100, 23);
checkBox3.Checked = true;
toolTip1.SetToolTip(checkBox3, "指示在同辈节点之间以及父节点与子节点之间是否显示连线。");
checkBox3.Click += new EventHandler(CheckBox3_click); imageList1.ImageStream = (System.Windows.Forms.ImageListStreamer)resources.GetObject("imageList1.ImageStream");
imageList1.TransparentColor = (Color)System.Drawing.Color.Transparent; imageListComboBox.ForeColor = (Color)System.Drawing.SystemColors.WindowText;
imageListComboBox.Location = new System.Drawing.Point(88, 192);
imageListComboBox.TabIndex = 8;
imageListComboBox.Text = "";
imageListComboBox.Size = new System.Drawing.Size(120, 21);
imageListComboBox.SelectedIndexChanged += new EventHandler(imageListComboBox_SelectedIndexChanged);
imageListComboBox.Items.AddRange((object[])new object[] {"(无)", "系统图像", "位图"}); checkBox4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
checkBox4.Location = new System.Drawing.Point(16, 88);
checkBox4.TabIndex = 3;
checkBox4.CheckState = System.Windows.Forms.CheckState.Checked;
checkBox4.Text = "showRootLines";
checkBox4.Size = new System.Drawing.Size(120, 23);
checkBox4.Checked = true;
toolTip1.SetToolTip(checkBox4, "指示是否在根节点之间显示连线。");
checkBox4.Click += new EventHandler(CheckBox4_click); grpTreeView.Location = new System.Drawing.Point(248, 16);
grpTreeView.TabIndex = 1;
grpTreeView.TabStop = false;
grpTreeView.Text = "树视图";
grpTreeView.Size = new System.Drawing.Size(248, 264); checkBox2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
checkBox2.Location = new System.Drawing.Point(16, 40);
checkBox2.TabIndex = 1;
checkBox2.Text = "hotTracking";
checkBox2.Size = new System.Drawing.Size(100, 23);
toolTip1.SetToolTip(checkBox2, "指示当鼠标移到节点上时,节点是否提供反馈。");
checkBox2.Click += new EventHandler(CheckBox2_click); label1.Location = new System.Drawing.Point(16, 194);
label1.TabIndex = 7;
label1.TabStop = false;
label1.Text = "imageList:";
label1.Size = new System.Drawing.Size(56, 16); grpTreeView.Controls.Add(checkBox7);
grpTreeView.Controls.Add(checkBox6);
grpTreeView.Controls.Add(checkBox5);
grpTreeView.Controls.Add(checkBox4);
grpTreeView.Controls.Add(checkBox3);
grpTreeView.Controls.Add(checkBox2);
grpTreeView.Controls.Add(label4);
grpTreeView.Controls.Add(indentUpDown);
grpTreeView.Controls.Add(label1);
grpTreeView.Controls.Add(imageListComboBox);
grpTreeView.Controls.Add(checkBox1);
this.Controls.Add(grpTreeView);
this.Controls.Add(directoryTree); } /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
public static void Main(string[] args) {
Application.Run(new TreeViewCtl());
} }