Link to home
Start Free TrialLog in
Avatar of Iys
Iys

asked on

C# WinForm drag and drop from listview to treeview

Hello experts.
I need help with implementing drag and drop from a listview to a tree view,
does anyone have a example.
Im using C# WinForm in VS2005.
Thanks,
Avatar of RubenvdLinden
RubenvdLinden

OK, create a listview and a treeview.

In my example, the listview is called listView1 and the treeview is called tvMain.
On the treeview, set AllowDrop to true.

Create an ItemDrag event on the listview:

private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            listView1.DoDragDrop(listView1.SelectedItems, DragDropEffects.Copy);
        }



In this example items from the listview are copied to the 'drop' object.
Now, create a DragEnter event on the treeview:

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



This was easy. Now the hard part starts. The following code adds the selected (and dragged) listview items to an existing node (make sure you have at least one node already in your treeview or the example will fail!)
Create a DragDrop event on the treeview:

private void tvMain_DragDrop(object sender, DragEventArgs e)
        {
            TreeNode n;

            if (e.Data.GetDataPresent("System.Windows.Forms.ListView+SelectedListViewItemCollection", false))
            {
                Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
                TreeNode dn = ((TreeView)sender).GetNodeAt(pt);
                ListView.SelectedListViewItemCollection lvi = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView+SelectedListViewItemCollection");

                foreach (ListViewItem item in lvi)
                {
                    n = new TreeNode(item.Text);
                    n.Tag = item;

                    dn.Nodes.Add((TreeNode)n.Clone());
                    dn.Expand();
                    n.Remove();
                }
            }
        }



The code adds a new treenode with the text of the listviewitem as a label.
The listviewitem object is copied to the Tag property of the treenode for later use.


I hope this helps! If you have any questions left on this example, feel free to ask.
Avatar of Iys

ASKER

Thanks this helps a lot,
How do I add a drag image acording to the image of the draged listview item?
Avatar of Iys

ASKER

RubenvdLinden: I have a weird bug in my app maybe you can help me.
I am trying to drag a treeview item in the tree itself but for some reason it does not allow me to drop on the control, although the allowdrop property is set to true, when I try to drag it to the form outside the control it allows me to drop.
what can be the reason, please help I am pretty much stuck.
thanks.
Please post your code so I can see what's wrong.
Avatar of Iys

ASKER

Thanks but I figured it out already.
Can you help me with changing the cursor image while draging a item?
ASKER CERTIFIED SOLUTION
Avatar of RubenvdLinden
RubenvdLinden

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Iys

ASKER

RubenvdLinden: Thanks for your help