C# – Adding a vertical scrollbar to a panel if drawn area is too large

cpanelsystem.graphicswinforms

I'm using WinForms and C#.

The application I am developing draws rows of rectangles (using g.DrawRectangle()) inside of a panel. The panel can hold 6 rectangles in width (I don't want to have horizontal scrolling). I control this within the application by counting the rectangles in the row, and then adding to the y value after the sixth rectangle.

Vertically, I want to be able to add infinite rectangles and scroll down to see them. Right now, the rectangles are being added, but the Panel doesn't scroll (they are just added off screen).

Is there a way to add a vertical scrollbar? I have tried setting the AutoScroll property to true, but that doesn't do anything.

Best Answer

The problem is that you are using graphics to draw on the panel. These are not controls, so they don't cause the panel to grow. you should create two panels - PanelA contains PanelB. PanelA has AutoScroll=true, but you draw on PanelB. As you draw, you also set the height of PanelB, so that when it gets bigger than PanelA, PanelA gets a scroll bar.

Related Topic