Click here to Skip to main content
15,038,345 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a panel in a user control or form to draw curves. The area of drawing width and height is larger the size of the panel. I set the AutoScroll = true, AutoScrollMinSize = panel1.Size (original size, i.e. 400X400). But the scroll bar does not show up. Further, I set the panel size to larger size, i.e. 800X800. And keep the AutoScrollMinSize = 400X400. The scroll bar still does not appear.

What could be wrong?


[Edit Moved from answer - Henry]
Thanks for the suggestion!

I created a windows form project and added a panel to the form. Then I added an event of panel.Paint. In the paint event handler, I draw a circle larger than the panel size.
panel.AutoScrollMinSize = new Size(504,337);

The circle's rectangle size is (800, 800). No scroll bar is displayed. Further, I set the panel.Size = new Size(504, 900) while keeping AutoScrollMinSize unchanged. There is still no scroll bar.
   public partial class Form1 : Form
       public Form1()
           panel1.AutoScrollMinSize = new System.Drawing.Size(panel1.Size.Width,
//           panel1.Size = new System.Drawing.Size(panel1.Size.Width, 900);
           panel1.Paint += new PaintEventHandler(panel1_Paint);
       void panel1_Paint(object sender, PaintEventArgs e)
           Pen penDraw = new Pen(new SolidBrush(Color.Black));
           Rectangle rectDraw = new Rectangle(0, 0, 800, 800);
           e.Graphics.DrawArc(penDraw, rectDraw, 0.0f, 360.0f);

Updated 29-Mar-21 19:00pm
Your code needed (please make it barely minimal but manifesting the problem). The problem seems easy.
Henry Minute 6-Feb-11 22:50pm
The OP responded by posting an answer.

I have moved it into the original question.
Henry Minute 6-Feb-11 22:54pm
When you reply to an answerer, you should either do so by adding a comment to their answer (use the green 'Add comment' widget at bottom right of the answer), or for instances like this (showing a code snippet) edit your original question to include it.

If you add a comment, the person who's answer you are commenting gets an email notification.

If you add an answer they don't.

You probably misunderstand Auto-scroll.
Here is the explanation:[^].

First, in case you hope Auto-scroll takes into account the bounding size of your rendered graphics: it won't. The panel will clip it exactly in the same way as without Auto-scroll; you reasoning "The circle's rectangle size is (800, 800)".

Then, AutoScrollMinSize should be more than Panel.Size. For example, if it is twice big as in Width and Height, it behaves like you have a 4-fold "virtual" paint area then the area you can see at once, at fixed scroll position.

Finally, Auto-scroll is designed to scroll children on your panel, not your rendered graphics. When you implement what you want, your graphics will be messed up when you scroll, because scroll does not invalidate you panel. If your handle the event Scroll.Scroll (as you should), you graphic... will not be moved with scroll, it be staying in the same place. To work around, you could move your rendering data as well, which in not effective. Look by yourself:

panel1.Width = 100;
//will scroll, but rendering... not as you expected!
panel1.Height = 100;
panel1.AutoScroll = true;
panel1.AutoScrollMinSize = new Size(200, 200);
panel1.Paint += (sender, ev) => {
   ev.Graphics.FillEllipse(Brushes.Red, new Rectangle(10, 10, 20, 40));
panel1.Scroll += (sender, ev) => {

This is not how such problem should be solved. Again, Auto-scroll is designed to scroll children on your panel, so you need put a child panel of bigger size than you parent panel. Then you should do you graphic rendering on bigger child panel but scroll a smaller parent panel. I did, it worked.

JOAT-MON 7-Feb-11 0:08am
+5 - I should have refreshed before posting! :)
gshen 7-Feb-11 12:42pm
Dear SAKryukov:
Thanks for your accurate explanation! Now, it is clear to me how to use the scrollable controls.
You're quite welcome!
Good luck and call again.
Espen Harlinn 7-Feb-11 15:39pm
Good effort, my 5
Thanks a lot.
The auto-scroll is detecting the region of the controls in the Panel.Controls collection. When you just draw on the panel it doe not detect that drawing in its Controls collection. You will need to add some kind user control to act as a canvas to draw on that will re-size itself to fully contain your drawing, then the region of that user control can trigger the panel.
I think you just confirmed my advice, please see. (I voted "5")
gshen 7-Feb-11 12:42pm
Dear SAKryukov:
Thanks for your accurate explanation! Now, it is clear to me how to use the scrollable controls.
Espen Harlinn 7-Feb-11 15:39pm
Good advice, my 5
I have discovered that if you have any components on board that auto-resize your Panel
or objects on your panel (or your entire program's objects) (such as "Resizer") then the scrollbars will not appear.
Maciej Los 30-Mar-21 6:57am
I'd avoid of answering such of old questions! First of all - there are 2 accepted answers and second of all - your answer does not improve anything.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900