Visual C#2005中的TreeView控件的使用
分类: Visual C# 20052007-08-09 17:253085人阅读评论(9)收藏举报
目录(?)[+]
最近在一个项目中遇到处理树状层次关系的数据,于是就暂时使用了TreeView来实现,我把其中的主要功能提了出来做成了一个Demo程序,在这里和大家分享,希望各位多多指教。下面就是实现的主要功能以及源码下载。
读取XML文件中的树状结构数据,并用TreeView控件呈现 在相同层级中前后/上下移动节点
删除指定值的节点
把TreeView的节点存储到XML文件中 开发环境:Windows XP SP2, Visual Studio 2005 with SP1
XML数据的组织结构
我的XML数据结构如下所示: <?xml version="1.0" encoding="utf-8"?> <TestTreeView>
<TestNode nodeText="Test1" nodeValue="a" />
<TestNode nodeText="Test2" nodeValue="b">
<TestNode nodeText="Test3" nodeValue="c">
<TestNode nodeText="Test4" nodeValue="d" />
<TestNode nodeText="Test5" nodeValue="e" />
</TestNode>
<TestNode nodeText="Test6" nodeValue="f" />
</TestNode>
<TestNode nodeText="Test7" nodeValue="c" />
</TestTreeView>
其中"nodeText"为在
TreeView中显示的节点文本,"nodeValue"为该节
点实际存储的值,而" a,b,c,d,e,f "为预定义的6个测试值 用TreeView控件呈现XML数据 把上述的XML文件的数据呈现到TreeView控件中主要使用了如下的递归处理:
#region 变量
private bool enabled = false;
private string strXmlPath = null;
private XmlDocument xmlDoc = null;
#endregion
#region 方法
/// <summary>
/// 根据XML文件加载树节点
/// </summary>
/// <param name="xmlPath">要加载的XML文件路径</param>
private void LoadTreeNodes(string xmlPath)
{
this.xmlDoc = new XmlDocument();
try
{
this.xmlDoc.Load(xmlPath);
XmlNodeList nodes = this.xmlDoc.SelectNodes("TestTreeView/TestNode");
this.treeMain.BeginUpdate();
this.treeMain.Nodes.Clear();
this.ConvertXmlNodeToTreeNode(nodes, this.treeMain.Nodes);
this.treeMain.EndUpdate();
}
catch (Exception ex)
{ MessageBox.Show("程序发生错误:" + ex.Message,
"异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ConvertXmlNodeToTreeNode(XmlNodeList
xmlNodes, TreeNodeCollection treeNodes) { foreach (XmlNode xmlNode in xmlNodes)
{
string nodeText = xmlNode.Attributes["nodeText"].Value;
string nodeValue = xmlNode.Attributes["nodeValue"].Value;
TreeNode newTreeNode = new TreeNode(nodeText);
newTreeNode.Tag = nodeValue;
if (xmlNode.HasChildNodes)
{
this.ConvertXmlNodeToTreeNode(xmlNode.ChildNodes, newTreeNode.Nodes);
}
treeNodes.Add(newTreeNode);
}
}
#endregion
在相同层级中前后/上下移动节点
这里的移动节点没有使用常见的定义一个Temp变量来存储临时数据,然后把要移动的2个数据交换位置。以“上移”为例,先把要移动的节点删除,再在该节点的前一个节点的位置插入该节点,在删除前把该节点存储在一个临时变量中,而“下移”则类似,下面是主要的代码:
// 在同级中上移当前节点
private void btnMoveUp_Click(object sender, EventArgs e)
{
TreeNode current = this.treeMain.SelectedNode;
TreeNode temp = null;
if (current != null)
{
temp = current;
TreeNode prev
= current.PrevNode; if (current.Parent != null) { TreeNode parent = current.Parent; parent.Nodes.Remove(current); parent.Nodes.Insert(prev.Index, temp); } else { this.treeMain.Nodes.Remove(current); this.treeMain.Nodes.Insert(prev.Index, temp); } this.treeMain.SelectedNode = temp; } }
// 在同级中下移当前节点
private void btnMoveDown_Click(object sender, EventArgs e)
{
TreeNode current = this.treeMain.SelectedNode;
if (current != null)
{
TreeNode next = current.NextNode;
if (next != null)
{
TreeNode temp = next;
if (current.Parent != null)
{
TreeNode parent = current.Parent;
parent.Nodes.Remove(next);
parent.Nodes.Insert(current.Index, temp);
}
else
{
this.treeMain.Nodes.Remove(next);
this.treeMain.Nodes.Insert(current.Index, temp);
}
this.treeMain.SelectedNode = temp;
this.treeMain.SelectedNode = current;
}
}
} 删除指定值的节点 TreeView节点实际存储的值保存在TreeNode.Tag中,选择要删除的值,然后搜索定位所有节点,最后删除这些节点,重点是遍历所有TreeView的节点搜索具有该值的节点,下面是主要代码:
// 定位指定值的节点,其中listNodesLocated是一个ListBox控件 // 列出所有定位到的节点的名称 private void LocateNodeWithValue(TreeNodeCollection nodes, string nodeValue) { foreach (TreeNode node in nodes)
{
if (node.Tag.ToString() == nodeValue)
{
this.listNodesLocated.Items.Add(node.Text);
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说公务员考试cTreeList与Xml操作结合在线全文阅读。
相关推荐: