winforms - C# ListView DragDrop Reordering not working -
im trying implement dragdrop
of listviewitems
re-order list.
the list not re-order or move items expected.
code
private void lstmodules_dragdrop(object sender, drageventargs e) { if (e.data.getdatapresent(typeof(listviewitem))) { lstmodules.alignment = listviewalignment.default; if (lstmodules.selecteditems.count == 0) return; var p = lstmodules.pointtoclient(new system.drawing.point(e.x, e.y)); listviewitem movetonewposition = lstmodules.getitemat(p.x, p.y); if (movetonewposition == null) return; listviewitem droptonewposition = (e.data.getdata(typeof(listview.selectedlistviewitemcollection)) listview.selectedlistviewitemcollection)[0]; listviewitem clonetonew = (listviewitem)droptonewposition.clone(); int index = movetonewposition.index; lstmodules.items.remove(droptonewposition); lstmodules.items.insert(index, clonetonew); lstmodules.alignment = listviewalignment.snaptogrid; } } private void lstmodules_dragenter(object sender, drageventargs e) { if (e.data.getdatapresent(typeof(listview.selectedlistviewitemcollection))) { e.effect = dragdropeffects.move; } } private void lstmodules_itemdrag(object sender, itemdrageventargs e) { lstmodules.dodragdrop(lstmodules.selecteditems, dragdropeffects.move); }
recently have done code try this.
private listviewitem tempitem = null; private void lvtables_mousedown(object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { application.doevents(); tempitem = lvtables.getitemat(e.x, e.y); string sdata = tempitem.text + ":" + tempitem.name; dragdropeffects effect = dodragdrop(sdata, dragdropeffects.move); lvtables.refresh(); } } private void lvtables_dragenter(object sender, drageventargs e) { if (e.data.getdatapresent(dataformats.stringformat)) e.effect = dragdropeffects.move; else e.effect = dragdropeffects.none; } private void lvtables_dragover(object sender, drageventargs e) { if (tempitem != null && lvtables.items.contains(tempitem)) { application.doevents(); int iindex = -1; try { point p = lvtables.pointtoclient(new point(e.x, e.y)); iindex = lvtables.getitemat(p.x, p.y).index; } catch { } if (iindex > -1 && iindex != tempitem.index) { if (lvtables.items.contains(tempitem)) lvtables.items.remove(tempitem); lvtables.items.insert(iindex, tempitem); tempitem.selected = true; } } } private void lvtables_mouseup(object sender, mouseeventargs e) { if (tempitem != null) { lvtables.selecteditems.clear(); tempitem.selected = true; tempitem = null; } }
Comments
Post a Comment