Articles in this section
Category / Section

How to drag and drop the TreeNodeAdv between different WinForms TreeViewAdv?

1 min read

Drag and drop

TreeNodeAdv can be dragged and dropped between different TreeViewAdv that is placed in different forms by handling following events.

1. ItemDrag

2. DragOver

3. DragDrop

4. DragEnter

5. DragLeave

C#

// Occurs when TreeNodeAdv drags from the TreeViewAdv
private void treeViewAdv_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
    TreeViewAdv treeViewAdv = sender as TreeViewAdv;
    // Allow to move
    DragDropEffects result = treeViewAdv.DoDragDrop(node, DragDropEffects.Move);
}
// Occures drop the TreeNodeAdv to the TreeViewAdv
private void treeViewAdv_DragDrop(object sender, DragEventArgs e)
{
    TreeViewAdv treeView = sender as TreeViewAdv;
    // Get the destination and source node.
    TreeNodeAdv sourceNode = (TreeNodeAdv)e.Data.GetData(typeof(TreeNodeAdv));
    TreeNodeAdv destinationNode = this.treeViewDragHighlightTracker.HighlightNode;
    TreeViewDropPositions dropPosition = this.treeViewDragHighlightTracker.DropPosition;
    // Move the source node based on the tracked info.
    if (destinationNode != null)
    {
        sourceNode.Move(destinationNode, NodePositions.Next);                 
    }
        treeView.SelectedNode = sourceNode;
}

VB

' Occurs when TreeNodeAdv drags from the TreeViewAdv
Private Sub treeViewAdv_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs)
    Dim treeViewAdv As TreeViewAdv = CType(sender, TreeViewAdv)
    ' Allow to move
    Dim result As DragDropEffects = treeViewAdv.DoDragDrop(node, DragDropEffects.Move)
End Sub
' Occures drop the TreeNodeAdv to the TreeViewAdv
Private Sub treeViewAdv_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
     Dim treeView As TreeViewAdv = CType(sender, TreeViewAdv)
     ' Get the destination and source node.
     Dim sourceNode As TreeNodeAdv = CType(e.Data.GetData(GetType(TreeNodeAdv)), TreeNodeAdv)
     Dim destinationNode As TreeNodeAdv = Me.treeViewDragHighlightTracker.HighlightNode
     Dim dropPosition As TreeViewDropPositions = Me.treeViewDragHighlightTracker.DropPosition
     ' Move the source node based on the tracked info.
     If (Not (destinationNode) Is Nothing) Then
        sourceNode.Move(destinationNode, NodePositions.Next)
     End If
    treeView.SelectedNode = sourceNode
End Sub

Samples:

C#: TreeViewAdvDragDrop_C#

VB: TreeViewAdvDragDrop_VB

Reference link: https://help.syncfusion.com/windowsforms/treeview/drag-and-drop

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied