c# 中的可滚动表单,AutoScroll=true 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17789197/
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
Scrollable Form in c#, AutoScroll=true doesn't work
提问by jovanMeshkov
What are the rules which I have to respect to make the Form scrollable...
我必须遵守哪些规则才能使表单可滚动...
I simple set the Property AutoScroll
to true.
I also tried while Auto Scroll
is true, to set AutoSize
to true/false, but none of these worked... also tried to put Panel and added all components in there... still nothing...
我简单地将属性设置AutoScroll
为 true。我也试过 whileAuto Scroll
为真,设置AutoSize
为真/假,但这些都不起作用......还尝试将面板放在那里并将所有组件添加到那里......仍然没有......
Maybe using V or HScrollBar
can help, but i really don't know how to link it with the Form...
也许使用V or HScrollBar
会有所帮助,但我真的不知道如何将它与表单链接...
form.AutoScroll = true;
formMainLayout.AutoScroll = true;
rootPanel.AutoScroll = true;
采纳答案by DonBoitnott
The content controls the scrolling. The scrollbars do not appear unless they are needed. Usually, there is a property available that you can set to force them to be visible always, and simply disabled until needed.
内容控制滚动。除非需要,否则不会出现滚动条。通常,您可以设置一个属性来强制它们始终可见,并在需要时简单地禁用。
The AutoScroll
property must be true
, as you have already found. But then the content of the scrollable control must force the parent control to display the scrollbars. This part is up to how the controls are embedded within the parent.
正如您已经发现的那样,该AutoScroll
属性必须true
为 。但随后可滚动控件的内容必须强制父控件显示滚动条。这部分取决于控件如何嵌入到父控件中。
Try these two experiments:
试试这两个实验:
Place a
Panel
on your form and dock it toFill
. Set theAutoScroll
property of the Panel totrue
. Into that panel, place aTextBox
and set it to dock asFill
as well. Also setMultiLine
totrue
. Run the application, and you will notice that the size of both is simply using the available space...no scrolling can occur because neither thePanel
, nor itsTextBox
become larger than the space they occupy.Perform the same steps as in #1, but this time, do not dock the
TextBox
. Instead, set it to a large size, something that you know will be larger than the amount ofPanel
that is visible. Running the application should now produce a scrollingPanel
.
将 a
Panel
放在表单上并将其停靠到Fill
。将AutoScroll
面板的属性设置为true
。在该面板中,放置 aTextBox
并将其设置为停靠Fill
。也设置MultiLine
为true
. 运行该应用程序,您会注意到两者的大小都只是使用了可用空间……不会发生滚动,因为 和 都Panel
不会TextBox
大于它们占用的空间。执行与 #1 中相同的步骤,但这次不要停靠
TextBox
. 相反,将其设置为大尺寸,您知道的内容将大于Panel
可见的数量。运行应用程序现在应该会产生一个滚动的Panel
.
Hopefully this little test helps to demonstrate what is controlling the scroll on a form.
希望这个小测试有助于演示是什么控制了表单上的滚动。
回答by varocarbas
The AutoScroll
property should work fine, but most likely you are not using it right: the bar appears only when required. Example: minimum Y of the Form
is 0 and minimum Y of one of the controls in it (a TextBox
) is -20.
该AutoScroll
属性应该可以正常工作,但很可能您没有正确使用它:该栏仅在需要时出现。示例: 的最小 YForm
为 0,其中一个控件 (a TextBox
) 的最小 Y为 -20。
If you want to include a scroll bar no matter what (controls inside the boundaries of the form or not), you can also do it. Sample code (from MSDN) for a vertical scroll bar:
如果您无论如何都想包含滚动条(控件在表单边界内与否),您也可以这样做。垂直滚动条的示例代码(来自 MSDN):
// Create and initialize a VScrollBar.
VScrollBar vScrollBar1 = new VScrollBar();
// Dock the scroll bar to the right side of the form.
vScrollBar1.Dock = DockStyle.Right;
// Add the scroll bar to the form.
Controls.Add(vScrollBar1);
回答by Sean
I was also having the same problem, I managed to fix it... All the child controls inside the panel had a Left& Rightanchor, and when I only set the anchor to Top, the scrollbars where working fine.
我也有同样的问题,我设法解决它......所有的面板内的子控件有一个左和右锚,当我只设置锚顶部,滚动条在那里工作的罚款。
I am not sure as to why the Leftand Rightanchor (of the child controls) forces the panel not to show scrollbars.
我不确定为什么(子控件的)左锚和右锚会强制面板不显示滚动条。
But anyways... hope this will help anyone as of this date.
但无论如何......希望这会在今天对任何人有所帮助。