Is it possible to have drag and drop from a ListView to a TreeView in Winforms?
If it's not possible, I can also use 2 TreeView controls. I just won't have a hierarchy in the second TreeView control. It's 开发者_JS百科gonna act like some sort of repository.
Any code sample or tutorial would be very helpful.
ListView
does not support drag-and-drop naturally, but you can enable it with a small bit of code:
http://support.microsoft.com/kb/822483
Here's an example that specifically does drag-and-drop from a ListView
to a TreeView
(it's an Experts Exchange link, so just wait a few seconds and then scroll to the bottom, where you'll find the answers):
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_22675010.html
Update: Code from the link:
- 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();
}
}
}
To change the cursor while dragging, you have to create a GiveFeedback event for the ListView control:
private void listView1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
e.UseDefaultCursors = false;
if (e.Effect == DragDropEffects.Copy)
{
Cursor.Current = new Cursor(@"myfile.ico");
}
}
myfile.ico
should be in the same directory as the .exe file.
This is just a simple example. You can extend it any way you like.
精彩评论