Dah Sra
Dah Sra

Reputation: 4445

DragEnter and DragDrop events not FIRING in treeview

I need to do drag and drop using treeview in c#.For that i heard 3 events are the most common 1.itemDrag 2.DragDrop and 3.DragEnter.

whereas here itemDrag event is firing for me while dragging from a treeview ,but rest both the events are not firing for me.Tried many solutions and now came here for an solution

    private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
    {
       string[] strItem = e.Item.ToString().Split(':');
       DoDragDrop(strItem[1], DragDropEffects.Copy | DragDropEffects.Move); }

the above method fires ,

    private void treeView1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }

the bove dragEnter event is not firing ,similarly the dragDrop event is also not firing.Why it's so??

Am dragging from the treeview and need to paste in PowerPoint or Word. (ie) treeview is something like an AddIn for Office Tools.

Regards, Arshad

Upvotes: 1

Views: 5677

Answers (2)

schei1
schei1

Reputation: 2487

From your provided code, I cannot see that you have implemented the DragDrop event nor have set the AllowDrop property on the controls involved, which is needed along with the two other events in order to perform a drag and drop.

Here is sample snatched directly from MSDN, which uses two treeviews to move nodes in between. Add a couple of treeviews, add some root and child nodes, and wire up these events. Remember the AllowDrop property.

I have added a couple of Debug.WriteLine() to help with the debugging while testing this. Can be hard to do with breakpoints ;-)

NOTE: For brewity I have not supplied the event wiring code, nor the code for creating sample nodes. If needed this can be found in the referenced article. Otherwise add those using the property window.

Sample code:

private void treeView_ItemDrag(object sender, ItemDragEventArgs e)
{
    Debug.WriteLine("ItemDrag fired!");
    DoDragDrop(e.Item, DragDropEffects.Move);
}

private void treeView_DragEnter(object sender, DragEventArgs e)
{
    Debug.WriteLine("TreeView DragEnter fired!");
    e.Effect = DragDropEffects.Move;
}

private void treeView_DragDrop(object sender, DragEventArgs e)
{
    Debug.WriteLine("TreeView DragDrop fired!");
    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: 0

aguertin
aguertin

Reputation: 494

Allow Drop AND..

Ok, assuming you have all of your events being declared/created in the Form_Load... e.i. :

tlAssemblies.DragDrop +=tlAssemblies_DragDrop;
tlAssemblies.MouseDown +=tlAssemblies_MouseDown;
tlAssemblies.MouseMove +=tlAssemblies_MouseMove;
tlAssemblies.DragEnter +=tlAssemblies_DragEnter;
tlAssemblies.DragOver += tlAssemblies_DragOver;

The drag Event is for when you fire the drag event inside your treeView which is why it is working. The dragEnter is when you enter the boundaries of a different* control.

i.e you want to drag from treeview 1 into treeview2.

If you are not trying to drag the item into a different control dragEnter is wrong.

Here is a drag drop event sample :

 private void tlAssemblies_DragDrop(object sender, DragEventArgs e)
 {
    if (sender == null)
        return;
    Point p = tlAssemblies.PointToClient(new Point(e.X, e.Y));
    TreeListNode dragNode = e.Data.GetData(typeof(TreeListNode)) as TreeListNode;
    TreeListNode targetNode = tlAssemblies.CalcHitInfo(p).Node;
    if (targetNode == null)
    {
        return;
    }

} Not sure if it is possible but you may want to change the dragEnter code you have and simply use it in the drag event i.e.

  e.Effect = DragDropEffects.Move; 

If you are not leaving the same control you are dragging both to and fro, might as well show the drag movement.

Another thing you could do is on the treeView_MouseMove Event.

 private void tlAssemblies_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && downHitInfo != null)
        {
            Size dragSize = SystemInformation.DragSize;
            Rectangle dragRect = new Rectangle(new Point(downHitInfo.HitPoint.X -     dragSize.Width / 2,
            downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize);
            if (!dragRect.Contains(new Point(e.X, e.Y)))
            {
                DataRow row = viewProduct.GetDataRow(downHitInfo.RowHandle);
                if(row != null)
                tlAssemblies.DoDragDrop(row, DragDropEffects.Move); 
                //viewProduct.GridControl.DoDragDrop(row, DragDropEffects.All); 
                downHitInfo = null;
                DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = true;
            }
        }
    }

Upvotes: 0

Related Questions