77范文网 - 专业文章范例文档资料分享平台

cTreeList与Xml操作结合

来源:网络收集 时间:2021-09-24 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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操作结合在线全文阅读。

cTreeList与Xml操作结合.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/gongwuyuan/1247011.html(转载请注明文章来源)

相关推荐:

Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: