c# - Is it possible to have drag and drop from a ListView to a TreeView in Winforms? -
if it's not possible, can use 2 treeview controls. won't have hierarchy in second treeview control. it's gonna act sort of repository.
any code sample or tutorial helpful.
listview
not support drag-and-drop naturally, can enable small bit of code:
http://support.microsoft.com/kb/822483
here's example drag-and-drop listview
treeview
(it's expert sex change link, wait few seconds , scroll bottom, you'll find answers):
http://www.experts-exchange.com/programming/languages/.net/visual_csharp/q_22675010.html
update: code link:
- create listview , treeview. ( in example, listview called listview1 , treeview called tvmain )
- on treeview, set allowdrop true.
- create itemdrag event on listview
private void listview1_itemdrag(object sender, itemdrageventargs e) { listview1.dodragdrop(listview1.selecteditems, dragdropeffects.copy); }
in example items listview copied 'drop' object. now, create dragenter event on treeview:
private void tvmain_dragenter(object sender, drageventargs e) { e.effect = dragdropeffects.copy; }
this easy. hard part starts. following code adds selected (and dragged) listview items existing node (make sure have @ least 1 node in treeview or example fail!)
create dragdrop event on 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 cursor while dragging, have create givefeedback event 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 in same directory .exe file.
this simple example. can extend way like.
Comments
Post a Comment