前两天被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();
}
}
}
{
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);
}
{
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交互。
这个方法很多,这里就不多介绍,贴出部分代码,请批评指正:
创建根节点
从XML中得到数据
向XML中写数据
TreeNodeToXml
XmlNodeToTreeNode XmlNodeToTreeNode
当添加了这些函数,接下来就是调用的问题了。从XML中得到数据
向XML中写数据
TreeNodeToXml
XmlNodeToTreeNode XmlNodeToTreeNode
这些是一些比较笨的方法,并且没有加异常处理另外考虑在也不全面,希望看到的,有好方法的不吝指教!谢谢!