.net - How to make TabPages draggable? -
i'd enable user rearrange tabpages order dragging , dropping. it'd cool enable user drag tabpages 1 tabcontrol another. both way in firefox , total commander. how achieve this?
reordering tabpages drag , drop - by ludwig b.
inspired http://dotnetrix.co.uk/tabcontrol.htm#tip7
private void tc_mousedown(object sender, mouseeventargs e) { // store clicked tab tabcontrol tc = (tabcontrol)sender; int hover_index = this.gethovertabindex(tc); if (hover_index >= 0) { tc.tag = tc.tabpages[hover_index]; } } private void tc_mouseup(object sender, mouseeventargs e) { // clear stored tab tabcontrol tc = (tabcontrol)sender; tc.tag = null; } private void tc_mousemove(object sender, mouseeventargs e) { // mouse button down? tab clicked? tabcontrol tc = (tabcontrol)sender; if ((e.button != mousebuttons.left) || (tc.tag == null)) return; tabpage clickedtab = (tabpage)tc.tag; int clicked_index = tc.tabpages.indexof(clickedtab); // start drag n drop tc.dodragdrop(clickedtab, dragdropeffects.all); } private void tc_dragover(object sender, drageventargs e) { tabcontrol tc = (tabcontrol)sender; // tab draged? if (e.data.getdata(typeof(tabpage)) == null) return; tabpage dragtab = (tabpage)e.data.getdata(typeof(tabpage)); int dragtab_index = tc.tabpages.indexof(dragtab); // hover on tab? int hovertab_index = this.gethovertabindex(tc); if (hovertab_index < 0) { e.effect = dragdropeffects.none; return; } tabpage hovertab = tc.tabpages[hovertab_index]; e.effect = dragdropeffects.move; // start of drag? if (dragtab == hovertab) return; // swap dragtab & hovertab - avoids toggeling rectangle dragtabrect = tc.gettabrect(dragtab_index); rectangle hovertabrect = tc.gettabrect(hovertab_index); if (dragtabrect.width < hovertabrect.width) { point tclocation = tc.pointtoscreen(tc.location); if (dragtab_index < hovertab_index) { if ((e.x - tclocation.x) > ((hovertabrect.x + hovertabrect.width) - dragtabrect.width)) this.swaptabpages(tc, dragtab, hovertab); } else if (dragtab_index > hovertab_index) { if ((e.x - tclocation.x) < (hovertabrect.x + dragtabrect.width)) this.swaptabpages(tc, dragtab, hovertab); } } else this.swaptabpages(tc, dragtab, hovertab); // select new pos of dragtab tc.selectedindex = tc.tabpages.indexof(dragtab); } private int gethovertabindex(tabcontrol tc) { (int = 0; < tc.tabpages.count; i++) { if (tc.gettabrect(i).contains(tc.pointtoclient(cursor.position))) return i; } return -1; } private void swaptabpages(tabcontrol tc, tabpage src, tabpage dst) { int index_src = tc.tabpages.indexof(src); int index_dst = tc.tabpages.indexof(dst); tc.tabpages[index_dst] = src; tc.tabpages[index_src] = dst; tc.refresh(); }
Comments
Post a Comment