Sometimes, I have a picturebox lets say 100×100. But the image it will display is actually 100×400.
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 🙁
I suppose you could add separate scrollbar controls and sync their
Scrollevents up with the offset at which the picture in thePictureBoxis drawn, but that sounds like actual work. There’s a better way.Add a
Panelcontrol to your form, and set itsAutoScrollproperty 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.Drag and drop your
PictureBoxcontrol inside of thePanelcontrol that you just added. ThePanelcontrol will then detect that one of its child controls is larger than its visible area and show scrollbars, thanks to theAutoScrollproperty. When the user moves the scrollbars, the portion of the image in yourPictureBoxthat is visible will be automatically adjusted. Magic.(The reason you have to use a
Panelcontrol as a container is becausePictureBoxdoes not inherit directly from theScrollableControlbase class, which is what provides theAutoScrollproperty.)