tree node - copy & paste

G

Guest

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!!
 
P

Picho

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
 
R

Ritesh Jain via DotNetMonster.com

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top