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

2009年4月27日星期一

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

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     descendant   of   the   dragged   node.   

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

 55             {

 56                 //   If   it   is     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     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     parent     

 80         //   or   ancestor   of     second   node.   

 81         private bool ContainsNode(TreeNode node1, TreeNode node2)

 82         {

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

 84             if (node2.Parent == nullreturn 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                //
107                 GetNode(TNode.Nodes);

108             }

109         }

110       

111 

0 评论:

发表评论