How can I show scrollbars on a PictureBox control?
Asked Answered
C

3

13

Sometimes, I have a picturebox lets say 100x100. But the image it will display is actually 100x400.

I don't want to increase the size of the picturebox itself. Instead, I would like to create a vertical scrollbar (or horizontal if needed).

I could not find a scrollbar in the toolbox, so I guess I have to code it. But, how? And I still wonder if I didn't make a mistake and didn't see the scrollbar in the toolbox. My apologies then :(

Cords answered 13/1, 2011 at 4:22 Comment(0)
O
21

I suppose you could add separate scrollbar controls and sync their Scroll events up with the offset at which the picture in the PictureBox is drawn, but that sounds like actual work. There's a better way.

  1. Add a Panel control to your form, and set its AutoScroll property to "True". This will cause the control to automatically show scrollbars when it contains content that lies outside of its currently visible bounds. The .NET Framework will take care of everything for you under the covers, without you having to write a single line of code.

  2. Drag and drop your PictureBox control inside of the Panel control that you just added. The Panel control will then detect that one of its child controls is larger than its visible area and show scrollbars, thanks to the AutoScroll property. When the user moves the scrollbars, the portion of the image in your PictureBox that is visible will be automatically adjusted. Magic.

(The reason you have to use a Panel control as a container is because PictureBox does not inherit directly from the ScrollableControl base class, which is what provides the AutoScroll property.)

Otoscope answered 13/1, 2011 at 7:1 Comment(2)
I had to set 'SizeMode' property of 'PictureBox' to 'AutoSize' to make this solution workHurtless
And I'd like to note that PictureBox cannot be docked in Panel.Kanter
P
5

I tried this and it worked well. But I noted that if the picturebox is docked in the panel, the picturebox is automatically set to the size of the parent panel, and can't be set larger (at least not in any way I could find). This defeats the purpose of the technique. So -- put the picturebox on the panel, but don't dock it, and it will work perfectly.

Pistoia answered 14/2, 2012 at 23:38 Comment(0)
I
2

There are no automatic scroll bars on a picture box, but you can add the VScrollBar (and HScrollBar) control to the form and handle the image scrolling manually by redrawing it at a different offset each time the Scroll event is fired.

Ise answered 13/1, 2011 at 4:27 Comment(3)
I'm way too lazy to do this. ;-)Otoscope
Your way IS a lot faster, but you don't get to enjoy the frustration of figuring out which way to offset the image when it scrolls or when to move the scroll bars where.Ise
If you need to base your image in center of the are are instead of top-left corner, then I think this is the only viable solution. Trick with Panel has its limitations – the image must grow from top-left corner.Kanter

© 2022 - 2024 — McMap. All rights reserved.