Resolved no drag & drop from treeview to listview

aw48

Active member
Joined
Feb 9, 2013
Messages
36
Location
Germany
Programming Experience
10+
hi,
I have a problem with draging a treeview item (itemtext to be exact) to a listview where the path should be display
the treeview is a component of a usercontrol classlibrary.
I use treeview_itemdrag to call DoDragDrop().
As soon as I move the mouse, the notallowed/notvalid - symbol is display (don't know how the small circle with the topleft to bottomright bar is called)
I've wasted a lot of time googling but with the samples I've I can't see that I'm anything missing
Any ideas ? thanks in advance
 
Have you gone through these documents?




Or you don't want to go through those documents, and just want somebody to do the understanding for you and give you code to just drop in (if you'll pardon the pun).
 
i did drag and drop before and I don' t see what code should be missing. from treeview I only need ItemDrag and with the listview I need DragEnter and DragDrop. but it's the treeview that doesn't allow any dragging
 
It would help us a lot if you shared your code.
 
The following works for me:
C#:
using System;
using System.Drawing;
using System.Windows.Forms;

public class TestForm : Form
{
    public TestForm()
    {
        var treeView = new TreeView()
        {
            Dock = DockStyle.Left
        };
        treeView.ItemDrag += (o, e) =>
        {
            var effect = DragDropEffects.None;
            if (e.Button == MouseButtons.Left)
                effect = DragDropEffects.Move;
            else if (e.Button == MouseButtons.Right)
                effect = DragDropEffects.Copy;
            DoDragDrop(e.Item, effect);
        };

        var listView = new ListView()
        {
            AllowDrop = true,
            Dock = DockStyle.Fill
        };
        listView.DragEnter += (o, e) => e.Effect = e.AllowedEffect;

        for (int x = 0; x < 3; ++x)
        {
            var node = treeView.Nodes.Add(String.Format("Node{0}", x * 4));
            for (int y = 1; y < 4; ++y)
                node = node.Nodes.Add(String.Format("Node{0}", x * 4 + y));
        }

        this.SuspendLayout();
        this.ClientSize = new Size(400, 300);
        this.Controls.Add(treeView);
        this.Controls.Add(listView);
        this.ResumeLayout(false);
    }

    [STAThread]
    static void Main()
    {
        Application.Run(new TestForm());
    }
}

Since you've done drag/drop before, recall that the drag effect is depending on where it is expected to be dropped, not on the source of the drag drop. That means that while the mouse is in the tree view, then the no drop cursor is shown.

So the case above, I don't want my tree view to accept drops so I don't set AllowDrop to true nor do I implement a DragEnter handler. On the other hand, I do want my list view to accept drops, so I set AllowDrop to true, and set the drop effect in DragEnter event handler.
 
// ************************************************************************************
private void tvwMain_ItemDrag(object sender, ItemDragEventArgs e)
// ************************************************************************************
{
TreeNode sourceNode = (TreeNode)e.Item;
DragDropEffects dde = tvwMain.DoDragDrop(sourceNode.Tag.ToString(), DragDropEffects.All);
}

at the moment the argument I use is 'sourceNode.Tag.ToString()'. originally I had 'sourceNode', then 'sourceNode.Text', 'sourceNode.Tag' , nothing works.
and remember, tvwMain is not a control of my program. it's a component of a usercontrol added to the form.
 
I actually never tried to drag onto the listview. that no drop cursor made me believe something's not working correctly. and everything's okay.
btw I don't need to distinguish between mousebuttons because I just want the treeview item's path in the listview
'That means that while the mouse is in the tree view, then the no drop cursor is shown' that's the solution
thank you
 
Back
Top Bottom