phadaphunk
phadaphunk

Reputation: 13273

Treeview drag and drop without destination node

I have two TreeView which are in seperate forms.

I implemented a Drag and Drop functionality so that I can drag from on tree to another and it works fine with the code I found here where I have to handle 3 events.

The thing is the setup of my windows looks like this :

enter image description here

As you can see, Window2 hides my Window1 and it's on purpose and needs to stay like this. The problem is my Drag and Drop is from Window2 to Window1 so I can't specify a destination node. Is there a way to simply drop into a TreeView without any destination node and say create a parent node next to the other ones ?


Links die examples don't :

private void Form1_Load(object sender, System.EventArgs e)
{
    this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView_ItemDrag);
    this.treeView2.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView_ItemDrag);
    this.treeView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView_DragEnter);
    this.treeView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView_DragEnter);
    this.treeView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView_DragDrop);
    this.treeView2.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView_DragDrop);   
}

private void treeView_ItemDrag(object sender,
        System.Windows.Forms.ItemDragEventArgs e)
{
    DoDragDrop(e.Item, DragDropEffects.Move);
}

private void treeView_DragEnter(object sender,
        System.Windows.Forms.DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

private void treeView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
    TreeNode NewNode;

    if(e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
    {
        Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
        TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);
            NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
        if(DestinationNode.TreeView != NewNode.TreeView)
        {     
            DestinationNode.Nodes.Add((TreeNode) NewNode.Clone());
            DestinationNode.Expand();
            //Remove Original Node
            NewNode.Remove();
        }
    }
}

Upvotes: 1

Views: 6607

Answers (1)

Bolu
Bolu

Reputation: 8786

Change treeview_dragdrop of both forms to the following:

   private void treeView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
    {
        TreeNode NewNode;

        if(e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
        {
            NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
            if (!(sender as TreeView).Nodes.Contains(NewNode))//Edit: add this if you don't want to add the same one again.
            {    
                 (sender as TreeView).Nodes.Add((TreeNode) NewNode.Clone());                 
                 NewNode.Remove(); //Edit: add this if you want to remove original one.
            } 
        }
    }

Upvotes: 2

Related Questions