TreeView
TreeView
概述
TreeView
控件用于以节点形式显示文本或数据。
TreeView
控件由
System.Windows.Forms.
TreeView
类来定义
;
而其中的节点
(
即
Node)
,由
System.Windows.Forms.
TreeNode
类来定义的。
所以当在程序中创建一个
TreeView
对象,其实只是创建了一个可以放置节点的
"
容器
"
。而在这个容器中加入一个节点,其实就是加入了从
TreeNode
类中创建的一个节点对象;同样删除一个节点,也就是删除一个
TreeNode
节点对象。
TreeView
主要属性:
Name
:获取或设置控件的名称。
Nodes
:获取分配给树视图控件的树节点集合。
Parent
:获取或设置控件的父容器
Index
:选定项的索引值
Tag
:获取或设置包含有关控件的数据的对象
TopNode
:获取树视图控件中第一个完全可见的树节点。
Text
:
指定节点显示的文本
SelectedNode
:当前被选择的节点
ExpandDepth:
指定默认情况下节点是否打开
Selected
:标明当前节点是否被选择的节点
ShowCheckBox
: 标明当前节点是否显示复选框
CheckedNodes:声明被选择的单个或者多个节点
ShowCheckBoxes
:声明是否显示复选框
ShowExpandCollapse
:声明是否显示/折叠状态
ShowLines: 声明节点间是否以线连接
Checked:标明节点上的复选框的选择状态
HotTracking:
指示当鼠标指针移过树节点标签时,树节点标签是否具有超链接的外观。
TreeView
主要方法:
1
定位根节点:treeView1.SelectedNode=treeView1.Nodes[0]
2
展开所有节点:treeView.SelectedNode.ExpandAll();
3
燕尾服开选定节点的下一级节点:treeView1.SelectedNode.Expand()
4
折叠所有节点:treeView.SelectedNode.Collapse();
5
添加节点:
tvw.Nodes.Add(new TreeNode("
根节点"
));
6
删除当前选中节点:tvw.Nodes[0].Nodes[tvw.SelectedNode.Index].Remove();
7
删除指定索引位置上的节点:tvw.Nodes[0].Nodes.RemoveAt(0);
TreeView
主要事件:
AfterCheck:选定树节点旁边的复选框后触发
AfterCollapse:折叠树节点后触发
AfterExpand:展开树节点后触发
AfterSelect:选定树节点后触发
BeforeCheck: 在将要更改选定树节点旁边的复选框时触发
BeforeCollapse: 在将要折叠树节点时触发
BeforeExpand: 在将要展开树节点时触发
BeforeSelect: 在将要更改选定树节点时触发
例题:实现如下所示的功能
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.IO;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
namespace
treeview
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public partial class Form1 : Form
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
public Form1()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
InitializeComponent();
FilltvwDirectory();
LoadImageToComBox();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 向树tvw添加盘符
/// </summary>
private void FilltvwDirectory()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//获取当前设备盘符,并添加到数组drivers[i]里面
string[] drives = Environment.GetLogicalDrives();
for (int i = 0; i < drives.Length; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
TreeNode cRoot = new TreeNode(drives[i]);
//TreeNode cRoot = new TreeNode();
//cRoot.Text = drives[i];
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
tvwDirectory.Nodes.Add(cRoot);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//添加该盘符下的文件夹
this.AddDirectories(cRoot);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 添加该盘符下的文件夹
/// </summary>
/// <param name="node"></param>
private void AddDirectories(TreeNode node)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//获取当前节点路径
DirectoryInfo dir = new DirectoryInfo(GetPathFromNode(node));
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//获取当前节点所在路径下的子文件夹
DirectoryInfo[] e = dir.GetDirectories();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
for (int i = 0; i < e.Length; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string name = e[i].Name;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//判断是否是上级目录
if (!name.Equals(".") && !name.Equals(".."))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
node.Nodes.Add(new TreeNode(name));
}
}
}
catch (Exception e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// MessageBox.Show(e.Message);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 递归调用产生节点对应文件夹的路径
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private string GetPathFromNode(TreeNode node)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (node.Parent == null)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return node.Text;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//递归调用产生节点对应文件夹的路径
//Combine(string path1,string path2)合并两个路径字符中
//如Path.Combine("C:windows","Kugoo");则生成C:/windows/Kugkoo
return Path.Combine(GetPathFromNode(node.Parent), node.Text);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// ComBox的内容
/// </summary>
private void LoadImageToComBox()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
cbxImageList.Items.AddRange(new object[] ...{ "系统图像", "图像A", "图像B", "图像C", "图像D" });
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 在将要展开树节点时触发的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tvwDirectory_BeforeExpand(object sender, TreeViewCancelEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
TreeNode nodeExpanding = (TreeNode)e.Node;
AddSubDirectories(nodeExpanding);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 调用AddDirectores()将该节点的子节点添加到树视图中该节点下
/// </summary>
/// <param name="node"></param>
private void AddSubDirectories(TreeNode node)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
for (int i = 0; i < node.Nodes.Count; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
AddDirectories(node.Nodes[i]);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 是否排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chkOrder_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (chkOrder.Checked == true)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.tvwDirectory.Sort();
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.tvwDirectory.Refresh();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 是否显示复选框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chkCheckBox_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (chkCheckBox.Checked == true)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tvwDirectory.CheckBoxes = true;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tvwDirectory.CheckBoxes = false;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 是否显示根级线
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chkRootLine_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (chkRootLine.Checked == true)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tvwDirectory.ShowRootLines = true;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tvwDirectory.ShowRootLines = false;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 是否显示同级线
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chkLine_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (chkLine.Checked == true)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tvwDirectory.ShowLines = true;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tvwDirectory.ShowLines = false;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 是否显示跟踪线
/// 指示当鼠标指针移过树节点标签时,树节点标签是否具有超链接的外观。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chkTrack_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (chkTrack.Checked == true)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tvwDirectory.HotTracking = true;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tvwDirectory.HotTracking = false;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 是否显示+ -号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chkPlus_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (chkPlus.Checked == true)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tvwDirectory.ShowPlusMinus = true;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tvwDirectory.ShowPlusMinus = false;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private void chkHideState_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (chkHideState.Checked == true)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tvwDirectory.HideSelection = true;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tvwDirectory.HideSelection = false;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// mouse单击节点时发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tvwDirectory_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
txtIndex.Text = tvwDirectory.SelectedNode.Index.ToString();
txtText.Text = tvwDirectory.SelectedNode.Text.ToString();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private void indentUpDown_ValueChanged(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//设置indentUpDown中微调范围
indentUpDown.Maximum = 250;
indentUpDown.Minimum = 150;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
tvwDirectory.Width = int.Parse( indentUpDown.Value.ToString());
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 为树设置图标
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cbxImageList_SelectedIndexChanged(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.tvwDirectory.ImageList = this.tvwimgList;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if (cbxImageList.SelectedIndex == 1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.tvwDirectory.SelectedImageIndex = 0;
this.tvwDirectory.ImageIndex = 0;
} else if(cbxImageList.SelectedIndex==2)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.tvwDirectory.SelectedImageIndex = 1;
this.tvwDirectory.ImageIndex = 1;
} else if (cbxImageList.SelectedIndex == 3)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.tvwDirectory.SelectedImageIndex = 2;
this.tvwDirectory.ImageIndex = 2;
}
else if (cbxImageList.SelectedIndex == 4)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.tvwDirectory.SelectedImageIndex = 3;
this.tvwDirectory.ImageIndex = 3;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.tvwDirectory.SelectedImageIndex = 5;
this.tvwDirectory.ImageIndex = 5;
}
}
}
}