1

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

Dah Sra
  • 4,107
  • 3
  • 30
  • 69
  • What type of application you're developing? Web/WinForms/...? – Ali Jul 11 '14 at 05:18
  • 2
    Have you set the `AllowDrop` property for the given controls? – scheien Jul 11 '14 at 05:23
  • @scheien: no i haven't.?? Should i?? – Dah Sra Jul 11 '14 at 05:42
  • You will have to set the AllowDrop property on all controls involved in the dragdrop. e.g. treeview1 and treeview2, or just treeview1 if you only reposition the element(s). – scheien Jul 11 '14 at 05:49
  • 1
    Just to clarify: The DragEnter should fire for the control the dragged item are moved to. If you drag an item out of your treeview, and then return with it, the event would fire. You would however need to have the AllowDrop set to true. – scheien Jul 11 '14 at 05:59

2 Answers2

0

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;
            }
        }
    }
aguertin
  • 496
  • 4
  • 18
  • @antony : Am dragging from the treeview and need to paste in PowerPoint or Word. (ie) treeview is something like an AddIn. – Dah Sra Jul 11 '14 at 05:45
0

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();
        }
    }
}
scheien
  • 2,457
  • 1
  • 19
  • 22