.net - How to scroll on increasing picturebox size in windows form c#? -
i have picturebox control in windows form movable rectangle drawing in it, picturebox size increase according position of rectangle, when picturebox size greater particular size scrollbar appear.
public void picbox_mousedown(object sender, mouseeventargs e) { if (rect.contains(new point(e.x, e.y))) { mmove = true; } oldx = e.x; oldy = e.y; } public void picbox_mouseup(object sender , mouseeventargs e) { mmove = false; } public void picbox_mousemove(object sender,mouseeventargs e) { if (mmove) { picbox.cursor = cursors.cross; rect.x = rect.x + e.x - oldx; rect.y = rect.y + e.y - oldy; } oldx = e.x; oldy = e.y; picbox.invalidate(); } public void picbox_mousepaint(object sender, painteventargs e) { picbox.invalidate(); picbox.size = new size((rect.width + rect.x) + 10, (rect.height + rect.y) + 10); draw(e.graphics); }
on first display of form picturebox
after dragging rectangle size of picturebox increases , scroll bar appears
everything works fine here problems no matter how size of picturebox scroll bar works fine when drag down,but when rectangle dragged , if edges of picturebox meets form whole scroll bar reset normal, looking @ pictures below able understand want say
how solve this, there way increase scroll according width of picturebox ? new @ this.
the rectangle acting if the x , y co-ordinates tracked respect winform not picturebox.
can make picturebox parent drawing?
i'm afraid isn't possible.
in winforms scrollbars
bring things view have overflown container. if 'underflow', ie if top
and/or left
values negative no scrollbars
show , if (because of additional overflow) won't bring them back.
the minimum scroll position zero
.
so have make sure not move things negative!
you can enforce scrollbars
adding dummy controls allow scroll picturebox
or left out of sight, not back!
if really really really want can detect situation , manually add scrollbar
controls , code them move picturebox
down/right..
since asking, here example play with. note though working coded scrollbars has numerous pitfalls..
- one of them how handle situation when real
scrollbar
shows well? in example solve not adding scrollbar panel overlaying it; real scrollbar hidden while coded 1 showing. - another problem numbers: how many pixels should content moved? don't let uses scroll anywhere , when happens move picturebox way , coded scrollbar disappears..
- as last point of warning should noted, coded
scrollbar
lets things controls have; it won't allow scroll negative realm! guess like. think impossible!
so, suggest don't use code below, unless ok bringing picturebox.top
zero!
// variable @ class level: vscrollbar vscroll = null; // move negative testing: picturebox1.top = -15; // check see if need vscrollbar scrollcheck(); void scrollcheck() { if (picturebox1.top < 0) { if (vscroll == null ) { vscroll = new vscrollbar(); vscroll.parent = panel1.parent; vscroll.scroll += vscroll_scroll; vscroll.bringtofront(); } vscroll.location = new point(panel1.right - vscroll.width - 2, panel1.top + 1); vscroll.height = panel1.clientsize.height - 2; vscroll.value = vscroll.maximum; vscroll.show(); } else { vscroll.hide(); } } void vscroll_scroll(object sender, scrolleventargs e) { int delta = e.newvalue - e.oldvalue; if (delta < 0) { picturebox1.top = 0; scrollcheck(); } }
all in believe avoiding situation best..