treeview托拽和动态添加节点以及treeview和xml的交互的实现

         前两天被winform的treeview搞晕了,现在终于有时间了,把自己用到的知识简单的写出来。供和我一样初用winform 的treeview的同志批评指正。
        一。treeview的托拽的实现(树与树之间的):
            首先,你要两棵树了,以treeview1和treeview2来说。设置其allowdrop属性为true。
               添加拖拽函数

None.gif private   void  treeView_ItemDrag( object  sender, System.Windows.Forms.ItemDragEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            DoDragDrop(e.Item, DragDropEffects.Move); 
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  treeView_DragEnter( object  sender, System.Windows.Forms.DragEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif         e.Effect 
= DragDropEffects.Move;
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  treeView_DragDrop( object  sender, System.Windows.Forms.DragEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            TreeNode NewNode; 
InBlock.gif            
if(e.Data.GetDataPresent("System.Windows.Forms.TreeNode"false)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Point pt 
= ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
InBlock.gif                TreeNode DestinationNode 
= ((TreeView)sender).GetNodeAt(pt); 
InBlock.gif                NewNode 
= (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode"); 
InBlock.gif                
if(DestinationNode.TreeView != NewNode.TreeView) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DestinationNode.Nodes.Add((TreeNode) NewNode.Clone());
InBlock.gif                    DestinationNode.Expand(); 
ExpandedSubBlockEnd.gif                }
 
ExpandedSubBlockEnd.gif            }
 
ExpandedBlockEnd.gif        }

None.gif

这是实现拖拽在主要代码,接下来就是初始化的时候构造几个节点,在form的page_load函数里加入

None.gif private   void  my1_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {        
InBlock.gif            TreeNode ParentNode1; 
InBlock.gif            TreeNode ParentNode2; 
InBlock.gif            ParentNode1 
= treeView1.Nodes.Add("WTS"); 
InBlock.gif            ParentNode2.Tag 
= "";
InBlock.gif            ParentNode1.Nodes.Add(
"WTS1"); 
InBlock.gif            ParentNode1.Nodes[
0].Tag = "";
InBlock.gif            ParentNode1.Nodes.Add(
"WTS2");    
InBlock.gif            ParentNode1.Nodes[
1].Tag = "";
InBlock.gif            ParentNode1.Expand(); 
InBlock.gif            ParentNode2 
= treeView2.Nodes.Add("wts");
InBlock.gif            ParentNode2.Tag 
= "";
InBlock.gif            ParentNode2.Nodes.Add(
"wts1"); 
InBlock.gif            ParentNode2.Nodes[
0].Tag = "";
InBlock.gif            ParentNode2.Nodes.Add(
"wts2"); 
InBlock.gif            ParentNode2.Nodes[
1].Tag = "";
InBlock.gif            ParentNode2.Expand(); 
InBlock.gif            
this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView_ItemDrag); 
InBlock.gif            
this.treeView2.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView_ItemDrag); 
InBlock.gif            
this.treeView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView_DragEnter); 
InBlock.gif            
this.treeView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView_DragEnter); 
InBlock.gif            
this.treeView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView_DragDrop); 
InBlock.gif            
this.treeView2.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView_DragDrop); 
ExpandedBlockEnd.gif        }

就可以了。
   二。动态添加节点及和XML交互。
           这个方法很多,这里就不多介绍,贴出部分代码,请批评指正:

ContractedBlock.gif ExpandedBlockStart.gif 创建根节点 #region 创建根节点
InBlock.gif        
private TreeNode CreateNewNode()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TreeNode node 
= new TreeNode(this.txtTitle.Text.Trim());
InBlock.gif            node.Tag 
= "";
InBlock.gif            
return node;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion
 
ContractedBlock.gifExpandedBlockStart.gif        
从XML中得到数据 #region 从XML中得到数据
InBlock.gif        
private void GetDataFromXML ()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//从XML中读取数据到TreeView            
InBlock.gif
            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlDocument xmlDoc 
= new XmlDocument();
InBlock.gif                xmlDoc.Load(
"Menu.xml");
InBlock.gif
InBlock.gif                XmlNodeList xmlNodes 
= xmlDoc.DocumentElement.ChildNodes;
InBlock.gif
InBlock.gif                
this.treeView1.BeginUpdate();
InBlock.gif                
this.treeView1.Nodes.Clear();
InBlock.gif                XmlNodeToTreeNode(xmlNodes, 
this.treeView1.Nodes);
InBlock.gif                
this.treeView1.EndUpdate();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion
 
ContractedBlock.gifExpandedBlockStart.gif        
向XML中写数据 #region 向XML中写数据
InBlock.gif        
private void InsertDataIntoXML ()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//将TreeView保存到XML文件中            
InBlock.gif
            XmlDocument doc = new XmlDocument();
InBlock.gif            doc.LoadXml(
"<Menu></Menu>");
InBlock.gif            XmlNode root 
= doc.DocumentElement;
InBlock.gif            doc.InsertBefore(doc.CreateXmlDeclaration(
"1.0""utf-8""yes"), root);
InBlock.gif            TreeNodeToXml(
this.treeView1.Nodes, root);
InBlock.gif            doc.Save(
"Menu.xml");
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion
 
ContractedBlock.gifExpandedBlockStart.gif        
TreeNodeToXml #region TreeNodeToXml
InBlock.gif        
private void TreeNodeToXml(TreeNodeCollection treeNodes, XmlNode xmlNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlDocument doc 
= xmlNode.OwnerDocument;
InBlock.gif            
foreach (TreeNode treeNode in treeNodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlNode element 
= doc.CreateNode("element""Item""");
InBlock.gif                XmlAttribute attr 
= doc.CreateAttribute("WTS");
InBlock.gif                treeNode.Tag 
="";
InBlock.gif                attr.Value 
= treeNode.Text;
InBlock.gif                element.Attributes.Append(attr);
InBlock.gif                element.AppendChild(doc.CreateCDataSection(treeNode.Tag.ToString()));
InBlock.gif                xmlNode.AppendChild(element);
InBlock.gif
InBlock.gif                
if (treeNode.Nodes.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    TreeNodeToXml(treeNode.Nodes, element);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion
 
ContractedBlock.gifExpandedBlockStart.gif        
XmlNodeToTreeNode #region XmlNodeToTreeNode
InBlock.gif        
private void XmlNodeToTreeNode(XmlNodeList xmlNode, TreeNodeCollection treeNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach (XmlNode var in xmlNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (var.NodeType != XmlNodeType.Element)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }

InBlock.gif                TreeNode newTreeNode 
= new TreeNode();
InBlock.gif                newTreeNode.Text 
= var.Attributes["WTS"].Value;            
InBlock.gif                
if (var.HasChildNodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (var.ChildNodes[0].NodeType == XmlNodeType.CDATA)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        newTreeNode.Tag 
= var.ChildNodes[0].Value;
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    XmlNodeToTreeNode(var.ChildNodes, newTreeNode.Nodes);
ExpandedSubBlockEnd.gif                }

InBlock.gif                treeNode.Add(newTreeNode);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion
 XmlNodeToTreeNode

 

当添加了这些函数,接下来就是调用的问题了。

   这些是一些比较笨的方法,并且没有加异常处理另外考虑在也不全面,希望看到的,有好方法的不吝指教!谢谢!

转载于:https://www.cnblogs.com/wts/archive/2007/03/26/688760.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值