TreeView Drag'n'Drop Nodes

Welcome to Wisej Support.
Questions, suggestions and plain old bugs - are all welcome here!
0
0

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

 

  • You must to post comments
0
0

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

  • You must to post comments
0
0

Hi Alex,

you can find an explanation and solution in an old thread by Tiago:

http://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

  • You must to post comments
Showing 2 results
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Attachment
File Name Size
There are currently no files uploaded.
Maximum number of files 1, maximum file size 3.9MB.
Supported file formats: zip pdf gif jpg bmp png cs vb theme json