登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

hurt0759的个人主页

人生常态--跋涉.人生暂态--歇息.

 
 
 

日志

 
 

C# TreeView上节点的拖动和节点的遍历  

2009-10-29 12:32:28|  分类: IT |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1 private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)

  2         {

  3             //   Move   the   dragged   node   when   the   left   mouse   button   is   used.   

  4             if (e.Button == MouseButtons.Left)

  5             {

  6                 DoDragDrop(e.Item, DragDropEffects.Move);

  7             }

  8 

  9             //   Copy   the   dragged   node   when   the   right   mouse   button   is   used.   

 10             else if (e.Button == MouseButtons.Right)

 11             {

 12                 DoDragDrop(e.Item, DragDropEffects.Copy);

 13             }

 14         } 

 15 

 16  

 17 

 18     //   Set   the   target   drop   effect   to   the   effect     

 19    //   specified   in   the   ItemDrag   event   handler.   

 20 

 21   private void treeView1_DragEnter(object sender, DragEventArgs e)

 22         {

 23             e.Effect = e.AllowedEffect;

 24         } 

 25 

 26  

 27 

 28   //   Select   the   node   under   the   mouse   pointer   to   indicate   the     

 29         //   expected   drop   location.   

 30         private void treeView1_DragOver(object sender, DragEventArgs e)

 31         {

 32             //   Retrieve   the   client   coordinates   of   the   mouse   position.   

 33             Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));

 34 

 35             //   Select   the   node   at   the   mouse   position.   

 36             treeView1.SelectedNode = treeView1.GetNodeAt(targetPoint);

 37         } 

 38 

 39  

 40 

 41  private void treeView1_DragDrop(object sender, DragEventArgs e)

 42         {

 43             //   Retrieve   the   client   coordinates   of   the   drop   location.   

 44             Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));

 45 

 46             //   Retrieve   the   node   at   the   drop   location.   

 47             TreeNode targetNode = treeView1.GetNodeAt(targetPoint);

 48 

 49             //   Retrieve   the   node   that   was   dragged.   

 50             TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode));

 51 

 52             //   Confirm   that   the   node   at   the   drop   location   is   not     

 53             //   the   dragged   node   or   a   descendant   of   the   dragged   node.   

 54             if (!draggedNode.Equals(targetNode) && !ContainsNode(draggedNode, targetNode))

 55             {

 56                 //   If   it   is   a   move   operation,   remove   the   node   from   its   current     

 57                 //   location   and   add   it   to   the   node   at   the   drop   location.   

 58                 if (e.Effect == DragDropEffects.Move)

 59                 {

 60                     draggedNode.Remove();

 61                     targetNode.Nodes.Add(draggedNode);

 62                 }

 63 

 64                 //   If   it   is   a   copy   operation,   clone   the   dragged   node     

 65                 //   and   add   it   to   the   node   at   the   drop   location.   

 66                 else if (e.Effect == DragDropEffects.Copy)

 67                 {

 68                     targetNode.Nodes.Add((TreeNode)draggedNode.Clone());

 69                 }

 70 

 71                 //   Expand   the   node   at   the   location     

 72                 //   to   show   the   dropped   node.   

 73                 targetNode.Expand();

 74             }

 75         } 

 76 

 77  

 78 

 79         //   Determine   whether   one   node   is   a   parent     

 80         //   or   ancestor   of   a   second   node.   

 81         private bool ContainsNode(TreeNode node1, TreeNode node2)

 82         {

 83             //   Check   the   parent   node   of   the   second   node.   

 84             if (node2.Parent == null) return false;

 85             if (node2.Parent.Equals(node1)) return true;

 86 

 87             //   If   the   parent   node   is   not   null   or   equal   to   the   first   node,     

 88             //   call   the   ContainsNode   method   recursively   using   the   parent   of     

 89             //   the   second   node.   

 90             return ContainsNode(node1, node2.Parent);

 91         }   

 92 

 93  

 94 

 95 //节点的遍历

 96 

 97  //调用   

 98             TreeNodeCollection tc = treeView1.Nodes;

 99             GetNode(tc); 

100 

101  public void GetNode(TreeNodeCollection tc)

102         {         

103             foreach (TreeNode TNode in tc)

104             {

105                //操作TNode如获得TNode的相关属性

106                //C TreeView上节点的拖动和节点的遍历 - michael - hurt0759的个人主页

107                 GetNode(TNode.Nodes);

108             }

109         }

110       

111 

  评论这张
 
阅读(1122)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018