Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / December 2004

Tip: Looking for answers? Try searching our database.

tree node - copy & paste

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Samir - 20 Dec 2004 11:09 GMT
How to copy the selected tree node along with its children and paste the same
under another selected tree node in same treeview using C# ?

Could you please give me a code snippets for the above said problem?

Thanx everyone!!
Picho - 20 Dec 2004 21:47 GMT
Samir,

A treeenode can only belong to one treeview.
the best way to do this will be to use the TreeNode.Clone() method and then
add the cloned node to the new parent node.

picho

> How to copy the selected tree node along with its children and paste the
> same
[quoted text clipped - 3 lines]
>
> Thanx everyone!!
Ritesh Jain - 21 Dec 2004 14:28 GMT
Hi ,
u can do this Copy Paste operation Using the DragDrop Functionality of the TreeView Control........Here is some code which will Help u...

Note : U must set AllowDrop of TreeView Control to True.

Add the Following Event of Tree View in ur Code........

1>

Public Sub TreeView1_ItemDrag(ByVal sender As System.Object, _
       ByVal e As System.Windows.Forms.ItemDragEventArgs) _
       Handles TreeView1.ItemDrag
       
       'Set the drag node and initiate the DragDrop
       DoDragDrop(e.Item, DragDropEffects.Move)
       
End Sub

2>

Public Sub TreeView1_DragEnter(ByVal sender As System.Object, _
       ByVal e As System.Windows.Forms.DragEventArgs) _
       Handles TreeView1.DragEnter

       'See if there is a TreeNode being dragged
       If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
           True) Then
           'TreeNode found allow move effect
           e.Effect = DragDropEffects.Move
       Else
           'No TreeNode found, prevent move
           e.Effect = DragDropEffects.None
       End If

   End Sub

3>

Public Sub TreeView1_DragOver(ByVal sender As System.Object, _
       ByVale As DragEventArgs) _
       Handles TreeView1.DragOver

       'Check that there is a TreeNode being dragged
       If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
              True) = False Then Exit Sub

       'Get the TreeView raising the event (incase multiple on form)
       Dim selectedTreeview As TreeView = CType(sender, TreeView)

       'As the mouse moves over nodes, provide feedback to
       'the user by highlighting the node that is the
       'current drop target
       Dim pt As Point = _
           CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
       Dim targetNode As TreeNode = selectedTreeView.GetNodeAt(pt)

       'See if the targetNode is currently selected,
       'if so no need to validate again
       If Not (selectedTreeview.SelectedNode Is targetNode) Then
           'Select the    node currently under the cursor
           selectedTreeview.SelectedNode = targetNode

           'Check that the selected node is not the dropNode and
           'also that it is not a child of the dropNode and
           'therefore an invalid target
           Dim dropNode As TreeNode = _
               CType(e.Data.GetData("System.Windows.Forms.TreeNode"), _
               TreeNode)
               
               Do Until targetNode Is Nothing
                   If targetNode Is dropNode Then
                       e.Effect = DragDropEffects.None
                       Exit Sub
                   End If
                   targetNode = targetNode.Parent
               Loop
           End If

           'Currently selected node is a suitable target
           e.Effect = DragDropEffects.Move
       End If

   End Sub

4>

Public Sub TreeView1_DragDrop(ByVal sender As System.Object, _
       ByVal e As System.Windows.Forms.DragEventArgs) _
       Handles TreeView1.DragDrop

       'Check that there is a TreeNode being dragged
       If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
             True) = False Then Exit Sub

       'Get the TreeView raising the event (incase multiple on form)
       Dim selectedTreeview As TreeView = CType(sender, TreeView)

       'Get the TreeNode being dragged
       Dim dropNode As TreeNode = _
             CType(e.Data.GetData("System.Windows.Forms.TreeNode"), _
             TreeNode)

       'The target node should be selected from the DragOver event
       Dim targetNode As TreeNode = selectedTreeview.SelectedNode

       'Remove the drop node from its current location
       dropNode.Remove()

       'If there is no targetNode add dropNode to the bottom of
       'the TreeView root nodes, otherwise add it to the end of
       'the dropNode child nodes
       If targetNode Is Nothing Then
           selectedTreeview.Nodes.Add(dropNode)
       Else
           targetNode.Nodes.Add(dropNode)
       End If

       'Ensure the newley created node is visible to
       'the user and select it
       dropNode.EnsureVisible()
       selectedTreeview.SelectedNode = dropNode

   End Sub

If u want u can Escape step 3

I hope this will help u............

Regards,
Ritesh

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.