treeview托拽和动态添加节点以及treeview和xml的交互的实现
http://www.cnblogs.com/wts/archive/2007/03/26/688760.html
前两天被winform的treeview搞晕了,现在终于有时间了,把自己用到的知识简单的写出来。供和我一样初用winform 的treeview的同志批评指正。
一。treeview的托拽的实现(树与树之间的):
首先,你要两棵树了,以treeview1和treeview2来说。设置其allowdrop属性为true。
添加拖拽函数
private void treeView_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
DoDragDrop(e.Item, DragDropEffects.Move);
}
private void treeView_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void treeView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
TreeNode NewNode;
if(e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
{
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);
NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
if(DestinationNode.TreeView != NewNode.TreeView)
{
DestinationNode.Nodes.Add((TreeNode) NewNode.Clone());
DestinationNode.Expand();
}
}
}
这是实现拖拽在主要代码,接下来就是初始化的时候构造几个节点,在form的page_load函数里加入
private void my1_Load(object sender, System.EventArgs e)
{
TreeNode ParentNode1;
TreeNode ParentNode2;
ParentNode1 = treeView1.Nodes.Add("WTS");
ParentNode2.Tag = "";
ParentNode1.Nodes.Add("WTS1");
ParentNode1.Nodes[0].Tag = "";
ParentNode1.Nodes.Add("WTS2");
ParentNode1.Nodes[1].Tag = "";
ParentNode1.Expand();
ParentNode2 = treeView2.Nodes.Add("wts");
ParentNode2.Tag = "";
ParentNode2.Nodes.Add("wts1");
ParentNode2.Nodes[0].Tag = "";
ParentNode2.Nodes.Add("wts2");
ParentNode2.Nodes[1].Tag = "";
ParentNode2.Expand();
this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView_ItemDrag);
this.treeView2.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView_ItemDrag);
this.treeView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView_DragEnter);
this.treeView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView_DragEnter);
this.treeView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView_DragDrop);
this.treeView2.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView_DragDrop);
}
就可以了。
二。动态添加节点及和XML交互。
这个方法很多,这里就不多介绍,贴出部分代码,请批评指正:
创建根节点#region 创建根节点
private TreeNode CreateNewNode()
{
TreeNode node = new TreeNode(this.txtTitle.Text.Trim());
node.Tag = "";
return node;
}
#endregion
从XML中得到数据#region 从XML中得到数据
private void GetDataFromXML ()
{
//从XML中读取数据到TreeView
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Menu.xml");
XmlNodeList xmlNodes = xmlDoc.DocumentElement.ChildNodes;
this.treeView1.BeginUpdate();
this.treeView1.Nodes.Clear();
XmlNodeToTreeNode(xmlNodes, this.treeView1.Nodes);
this.treeView1.EndUpdate();
}
catch
{
}
}
#endregion
向XML中写数据#region 向XML中写数据
private void InsertDataIntoXML ()
{
//将TreeView保存到XML文件中
XmlDocument doc = new XmlDocument();
doc.LoadXml("<Menu></Menu>");
XmlNode root = doc.DocumentElement;
doc.InsertBefore(doc.CreateXmlDeclaration("1.0", "utf-8", "yes"), root);
TreeNodeToXml(this.treeView1.Nodes, root);
doc.Save("Menu.xml");
}
#endregion
TreeNodeToXml#region TreeNodeToXml
private void TreeNodeToXml(TreeNodeCollection treeNodes, XmlNode xmlNode)
{
XmlDocument doc = xmlNode.OwnerDocument;
foreach (TreeNode treeNode in treeNodes)
{
XmlNode element = doc.CreateNode("element", "Item", "");
XmlAttribute attr = doc.CreateAttribute("WTS");
treeNode.Tag ="";
attr.Value = treeNode.Text;
element.Attributes.Append(attr);
element.AppendChild(doc.CreateCDataSection(treeNode.Tag.ToString()));
xmlNode.AppendChild(element);
if (treeNode.Nodes.Count > 0)
{
TreeNodeToXml(treeNode.Nodes, element);
}
}
}
#endregion
XmlNodeToTreeNode#region XmlNodeToTreeNode
private void XmlNodeToTreeNode(XmlNodeList xmlNode, TreeNodeCollection treeNode)
{
foreach (XmlNode var in xmlNode)
{
if (var.NodeType != XmlNodeType.Element)
{
continue;
}
TreeNode newTreeNode = new TreeNode();
newTreeNode.Text = var.Attributes["WTS"].Value;
if (var.HasChildNodes)
{
if (var.ChildNodes[0].NodeType == XmlNodeType.CDATA)
{
newTreeNode.Tag = var.ChildNodes[0].Value;
}
XmlNodeToTreeNode(var.ChildNodes, newTreeNode.Nodes);
}
treeNode.Add(newTreeNode);
}
}
#endregion XmlNodeToTreeNode
当添加了这些函数,接下来就是调用的问题了。
这些是一些比较笨的方法,并且没有加异常处理另外考虑在也不全面,希望看到的,有好方法的不吝指教!谢谢!