Hi guys,
In another thread, Tiago says that he is now able to drag’n drop between TreeView nodes, so I must be missing something… My implementation:
tv.AllowDrag = true;
tv.AllowDrop = true;
………..
void tv_ItemDrag(object sender, ItemDragEventArgs e)
{
TreeNode DragNode = (TreeNode)e.Item;
DoDragDrop(DragNode, DragDropEffects.Move);
}
void tv_DragDrop(object sender, DragEventArgs e)
{
TreeNode DragNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode DestinationNode = tv.GetNodeAt(e.X, e.Y);
// dropping a node on itself is not allowed
if (DestinationNode != null && DestinationNode != DragNode)
{
……………………..
}
}
First of all I don’t find the tv.GetNodeAt(e.X, e.Y), which I need to get the drop target node. To make it compile, I tried as an alternative
TreeNode DestinationNode = (TreeNode)e.DropTarget; However, I haven’t manages to initiate DragDrop, in my ItemDrag event I get an error saying “Cannot initiate dragging on MyTree when AllowDrag is false. But I have explicitly set that to true.
Any ideas?
Best,
Alex
Hi Alex,
you can find an explanation and solution in an old thread by Tiago:
https://wisej.com/support/question/treeview-doesnt-honour-allowdrag
Please note that you have to call tv.DragDrop, otherwise you are calling the DragDrop on the form instead of the treeview.
Best regards
Frank
Thanks Frank and Tiago !
Tiago’s sample at the end of the thread is very complete. Actually I was doing something very similar, but the key point was the call to tv.DoDragDrop() instead of simple DoDragDrop(), which, as you Frank pointed out, refers to the Form and not the TreeView.
Best,
Alex