c# winform 程序TreeView
本人刚接触winform对TreeView 不熟悉。。谁能给源码介绍TreeView的用法。。增、删、改和右键功能。。
要求要在树的本身上进行操作。。
[最优解释]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace XMLOperate
{
public partial class Form1 : Form
{
//声明XML文档对象模型
XmlDocument document;
//选中的节点
TreeNode selectNode;
public Form1()
{
InitializeComponent();
//实例化文档模型
this.document = new XmlDocument();
}
//退出
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
//添加根元素还是子元素
bool isPater = true;
//添加
private void btnAppend_Click(object sender, EventArgs e)
{
try
{
//没果没有根元素
if (isPater)
{
string root = this.txtElement.Text.Trim();
this.trvXml.Nodes.Add(root);
this.document.LoadXml("<" + root + "></" + root + ">");
this.isPater = false;
this.btnInsert.Enabled = true;
this.btnRemove.Enabled = true;
this.btnReplace.Enabled = true;
this.btnSave.Enabled = true;
this.trvXml.SelectedNode = this.trvXml.Nodes[0];
this.txtElement.Clear();
}
else
{
if (this.trvXml.SelectedNode != null)
{
string element = this.txtElement.Text.Trim();
this.selectNode.Nodes.Add(element);
XmlNodeList nodelist = this.document.GetElementsByTagName(this.selectNode.Text);
int nodeCount = nodelist.Count;
//循环添加可能有多个元素
for (int i = 0; i < nodeCount; i++)
{
XmlNode node = this.document.CreateElement(element);
nodelist.Item(i).AppendChild(node);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//根据选中节点位置来更新树信息
private void trvXml_AfterSelect(object sender, TreeViewEventArgs e)
{
this.selectNode = e.Node;
}
//保存
private void btnSave_Click(object sender, EventArgs e)
{
//确定是否要保存
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
string filePath = this.saveFileDialog1.FileName;
this.document.Save(filePath);
MessageBox.Show("保存成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
//插入
private void btnInsert_Click(object sender, EventArgs e)
{
try
{
if (this.trvXml.SelectedNode != null)
{
//找到当前选中节点的索引
string element = this.txtElement.Text.Trim();
this.selectNode.Parent.Nodes.Insert(this.selectNode.Index + 1, element);
XmlNodeList nodelist = this.document.GetElementsByTagName(this.selectNode.Text);
int nodeCount = nodelist.Count;
//循环添加可能有多个元素
for (int i = 0; i < nodeCount; i++)
{
XmlNode node = this.document.CreateElement(element);
//将指定节点插入选中节点之后
nodelist.Item(i).ParentNode.InsertAfter(node, nodelist.Item(i));
}
this.txtElement.Clear();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//替换
private void btnReplace_Click(object sender, EventArgs e)
{
try
{
//找到当前选中节点的索引
string element = this.txtElement.Text.Trim();
//先添加进去再删除它
this.selectNode.Parent.Nodes.Insert(this.selectNode.Index,element);
this.trvXml.Nodes.Remove(this.selectNode);
XmlNodeList nodelist = this.document.GetElementsByTagName(this.selectNode.Text);
int nodeCount = nodelist.Count;
//循环添加可能有多个元素
for (int i = 0; i < nodeCount; i++)
{
XmlNode node = this.document.CreateElement(element);
//替换节点
nodelist.Item(i).ParentNode.RemoveChild(nodelist.Item(i));
nodelist.Item(i).ParentNode.InsertAfter(node, nodelist.Item(i));
}
this.txtElement.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//删除
private void btnRemove_Click(object sender, EventArgs e)
{
try
{
//如果全删除全完了,则需要重新添加根元素
if (this.trvXml.Nodes.Count < 0)
{
this.isPater = true;
}
this.trvXml.Nodes.Remove(this.selectNode);
XmlNodeList nodelist = this.document.GetElementsByTagName(this.selectNode.Text);
int nodeCount = nodelist.Count;
//循环添加可能有多个元素
for (int i = 0; i < nodeCount; i++)
{
nodelist.Item(i).ParentNode.RemoveChild(nodelist.Item(i));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
[其他解释]
http://www.ltesting.net/AutoTest/Others/200612/1360.htm
TreeView使用笔记
[其他解释]
我有个例子,需要联系我吧 QQ 88051668