$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
WinForms TreeView Control
Build complex navigation systems and display complex hierarchical structures
Home > WinForms UI Controls > User Manual > Customize Drag and Drop in C#

KetticTreeView Drag and Drop Support in C#.NET

KetticTreeView provides drag and drop support to expand the Windows application capabilities. With this drag and drop support, the developers can easily drag and drop nodes within the TreeView control, between Kettic WinForms UI controls, locate the node place, drag and drop nodes in other elements, scroll up and down a dragging node automatically, expand a dragging node automatically, and fine-tune specific nodes when dragging and dropping. In the C# tutorial below, we shall show how to customize the drag and drop of TreeView behavior and how to customize the control to copy the nodes as is dropped.

C# Create Custom Drag and Drop behavior for KetticTreeView

  • Create a descendant C# class of the TreeViewDragDropService and name it as CustomDragDrop
  • Expose the KetticTreeViewElement as a field in the CustomDragDrop C# class
  • Assign the created field to the constructor of the CustomDragDropService
  • Create one more field of type KetticTreeNode to keep the node that is dragged
  • Assign the KetticTreeNode to the PerformStart method and then clear it with the PerformStop method
  • Override the OnPreviewDragOver method to specify the conditions that will be applied
  • Add a logic for copying the selected nodes rather than move the nodes in the OnPreviewDragDrop override

C# Create Element for Applying Drag and Drop to KetticTreeView

When creating the custom drag and drop behavior, we can apply it to the KetticTreeView through the CreateDragDropService method of KetticTreeViewElement. The following C# code shows how to create a new element to achieve this.

class CustomTreeViewElement : KetticTreeViewElement
{
protected override Type ThemeEffectiveType
{
get
{
return typeof(KetticTreeViewElement);
}
}

protected override TreeViewDragDropService CreateDragDropService()
{
return new CustomDragDropService(this);
}
}

Apply Custom Drag and Drop to KetticTreeView in C#.NET

After creating the element, we can apply the CustomTreeViewElement in the tree view. This is achieved via passing a new instance of the element in the CreateTreeViewElement of KetticTreeView decendant C# class. The C# code below shows how to do this

class CustomTreeView : KetticTreeView
{
protected override KetticTreeViewElement CreateTreeViewElement()
{
return new CustomTreeViewElement();
}

public override string ThemeClassName
{
get
{
return typeof(KetticTreeView).FullName;
}
}
}

And then, we can populate the tree view and check the drag and drop functionality of KetticTreeView control. The following C# code demonstrates how to achieve this.

customTreeView1.AllowDragDrop = true;
customTreeView1.MultiSelect = true;

customTreeView2.AllowDragDrop = true;
customTreeView2.MultiSelect = true;

for (int i = 0; i < 10; i++)
{
customTreeView1.Nodes.Add("First tree node " + i);
customTreeView2.Nodes.Add("Second tree node " + i);
}

UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls