C# 面板自动滚动不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2048715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Panel autoscroll doesn't work
提问by Martijn
I have a panel with a picturebox on it. When the content of the picturebox is too large I want scrollbars. I've set the autoscroll set to true on the panel. But when the content of the picturebox is larger then the height of the panel/picturebox no scrollbars are shown.
我有一个带有图片框的面板。当图片框的内容太大时,我想要滚动条。我已将面板上的自动滚动设置为 true。但是当图片框的内容大于面板/图片框的高度时,不会显示滚动条。
My panel is anchored top, left, bottom, right. The picturebox is also anchored top, left, bottom, right.
我的面板固定在顶部、左侧、底部、右侧。图片框也锚定在顶部、左侧、底部、右侧。
采纳答案by Joey
You'll have to set the SizeMode
property to AutoSize
which causes the PictureBox to automatically resize to the picture's size.
您必须设置使 PictureBox 自动调整为图片大小的SizeMode
属性AutoSize
。
And I think you should drop the anchors and only anchor to the top left. The rest is taken care of automatically by the panel. Because otherwise the PictureBox would probably honor the anchors, being unable to resize itself larger than the panel it contains – leading to no scrollbars again.
我认为你应该放下锚点,只锚点到左上角。其余的由面板自动处理。因为否则 PictureBox 可能会尊重锚点,无法将自身调整为大于它包含的面板的大小 - 再次导致没有滚动条。
回答by Henrik
The picturebox should not be anchored bottom and right. Instead, resize it to the content to display.
图片框不应固定在底部和右侧。相反,将其调整为要显示的内容。
回答by serhio
From MSDN:
从MSDN:
There is currently a limitation in Windows Forms that prevents all classes derived from
ScrollableControl
from acting properly when bothRightToLeft
is enabled andAutoScroll
is set toTrue
. For example, let's say that you place a control such asPanel
— or a container class derived fromPanel
(such asFlowLayoutPanel
orTableLayoutPanel
) — on your form. If you setAutoScroll
on the container toTrue
and then set theAnchor
property on one or more of the controls inside of the container toRight
, then no scrollbar ever appears. The class derived fromScrollableControl
acts as ifAutoScroll
were set toFalse
.
当前在 Windows 窗体中存在一个限制,即
ScrollableControl
在RightToLeft
启用和AutoScroll
设置为True
. 例如,假设您在表单上放置了一个控件,例如Panel
— 或派生自Panel
(例如FlowLayoutPanel
或TableLayoutPanel
)的容器类。如果您将AutoScroll
容器True
设置Anchor
为Right
,然后将容器内一个或多个控件的属性设置为,则不会出现滚动条。派生自的类ScrollableControl
就像AutoScroll
被设置为False
.
回答by Sagin Joy
Change the Panel's Border Style to Fixed Single and then the Panel's Auto Scroll Property to True.
将面板的边框样式更改为固定单一,然后将面板的自动滚动属性更改为 True。
回答by Nittin Choudhary
For me below code helped in adding scrollbar:
对我来说,下面的代码有助于添加滚动条:
Panel2.Controls.Clear();
Panel2.AutoScroll = false;
Panel2.VerticalScroll.Enabled = true;
Panel2.VerticalScroll.Visible = true;
Panel2.AutoScroll = true;