简体   繁体   English

C#Winforms-具有矩形的可滚动面板

[英]c# winforms - scrollable panel with rectangles

I'm new to winforms and have tried to track down an answer to the following with no luck ... 我是winforms的新手,并试图在没有运气的情况下找到以下答案...

I have a panel in winforms and add to it a Rectangle that is wider than the panel itself. 我在winforms中有一个面板,并向其中添加了一个比面板本身宽的矩形。 I've set the panel AutoScroll property to true however the panels horizontal scrollbar never appears. 我已经将面板的AutoScroll属性设置为true,但是面板的水平滚动条从未出现。 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. 如果我在面板上添加一个Label控件,并为其提供一个文本值,该文本值将超出面板的边界-那么自动滚动将起作用,但不适用于矩形。

Many thanks. 非常感谢。

将AutoScrollMinSize属性设置为较大矩形的大小:

panel1.AutoScrollMinSize = new Size (400, 400)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM