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,
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,
ASKER
Thanks this helps a lot,
How do I add a drag image acording to the image of the draged listview item?
How do I add a drag image acording to the image of the draged listview item?
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.
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.
ASKER
Thanks but I figured it out already.
Can you help me with changing the cursor image while draging a item?
Can you help me with changing the cursor image while draging a item?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
RubenvdLinden: Thanks for your help
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(listV
}
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("Sy
{
Point pt = ((TreeView)sender).PointTo
TreeNode dn = ((TreeView)sender).GetNode
ListView.SelectedListViewI
foreach (ListViewItem item in lvi)
{
n = new TreeNode(item.Text);
n.Tag = item;
dn.Nodes.Add((TreeNode)n.C
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.