views:

188

answers:

1

Hi, I'm new to winforms and have tried to track down an answer to the following with no luck ...

I have a panel in winforms and add to it a Rectangle that is wider than the panel itself. I've set the panel AutoScroll property to true however the panels horizontal scrollbar never appears. Why is this? And how do I get the scrollbar to scroll?

Here is my code to add the rectangle:

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;

        Rectangle rec = new Rectangle(2, 2, 400, 40);
        g.DrawRectangle(new Pen(Color.Black), rec);
        g.FillRectangle(new SolidBrush(Color.Blue), rec);
    }

If I add a Label control to the panel and give it a text value that will go beyond the bounds of the panel - then the autoscroll works, but just not for a rectangle.

Many thanks.

+1  A: 

Set the AutoScrollMinSize property to the size of the larger rectangle:

panel1.AutoScrollMinSize = new Size (400, 400)
Joe Albahari
Thanks for this - this solution works fine. However the panel doesn't seem to always redraw properly when moving the scrollbar back and forth. Would you have any idea why this would happen? ... perhaps I'm in need a another stackoverflow question here :-)
Peanut
Make sure you set ResizeRedraw to true and draw relative to DisplayRectangle. For example: Rectangle r = DisplayRectangle; r.Inflate (-5, -5); e.Graphics.DrawRectangle (Pens.Blue, r);
Joe Albahari