C#:如何让用户控件正确地自动调整大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1356232/
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#: How to get a user control to properly auto size itself
提问by Svish
I have a UserControl
which consists of a Label
(Top), a FlowLayoutPanel
(Fill, TopDown flow and no wrap) and a Panel
(Bottom). The user control creates a number of controls, based on a list of stuff it gets, and adds them to the FlowLayoutPanel
.
我有一个UserControl
由Label
(顶部)、一个FlowLayoutPanel
(填充、自上而下流和无包装)和一个Panel
(底部)组成的。用户控件根据它获取的内容列表创建许多控件,并将它们添加到FlowLayoutPanel
.
How can I get this UserControl
to properly resize itself so that the FlowLayoutPanel
does not have any scroll bars? I have tried to use various combinations of AutoSize
and AutoSizeMode
on the FlowLayoutPanel
and the UserControl
itself, but I can't seem to get it working. Either I end up with something that doesn't resize itself at all, or it doesn't become big enough or it is squished down to almost nothing.
我怎样才能UserControl
让它正确调整自己的大小,以便FlowLayoutPanel
没有任何滚动条?我曾尝试使用的各种组合AutoSize
,并AutoSizeMode
在FlowLayoutPanel
与UserControl
本身,但我似乎无法得到它的工作。要么我最终得到的东西根本不会调整自己的大小,要么它变得不够大,要么被压扁成几乎没有。
采纳答案by Svish
Thanks for all the suggestions. The solution this time seemed to set AutoSize
to true
for both the FlowLayoutPanel
andthe UserControl
itself.
感谢所有的建议。该解决方案这段时间似乎设置AutoSize
到true
两者的FlowLayoutPanel
和的UserControl
本身。
Now, how to get the form which will contain this UserControl
as well as some other controls, that I can't quite figure out at the moment, but I guess that should be a separate question...
现在,如何获取将包含此UserControl
以及其他一些控件的表单,我目前无法弄清楚,但我想这应该是一个单独的问题......
回答by Mickel
You can use the Anchor- and Dock property of the UserControl to set options so that the edges of your control gets "glued" to some other parts of your UI. When the UI gets resized, your control will follow along!
您可以使用 UserControl 的 Anchor- 和 Dock 属性来设置选项,以便控件的边缘“粘合”到 UI 的其他部分。当 UI 调整大小时,您的控件将随之而来!
If you use anchors and dock on all controls in your user control and set them to dock the edges of the control, the controls will resize with the UserControl and you can now set anchors/dock to the UserControl also.
如果您在用户控件中的所有控件上使用锚点和停靠点并将它们设置为停靠控件的边缘,则控件将随 UserControl 调整大小,您现在也可以将锚点/停靠点设置到 UserControl。
回答by benPearce
I found that the best way to solve this was to use some code in the ControlAdded event on the flowlayoutpanel to adjust the height of the panel (the height because you were using the TopDown flow) to be:
我发现解决这个问题的最好方法是在 flowlayoutpanel 上的 ControlAdded 事件中使用一些代码来调整面板的高度(因为您使用的是 TopDown 流的高度)为:
e.Control.Location.Y + e.Control.Height + some margin
and to set the AutoSize property on the usercontrol itself to True.
并将用户控件本身的 AutoSize 属性设置为 True。
You would also need to do some of docking and anchoring suggestions as per Mickel's answer.
您还需要根据 Mickel 的回答做一些对接和锚定建议。
This functionality could be encapsulated in to your own control inherited from FlowLayoutPanel and overriding the ControlAdded method.
此功能可以封装到您自己的从 FlowLayoutPanel 继承的控件中并覆盖 ControlAdded 方法。
回答by Bryan Menard
Wrapping the FlowLayoutPanel
in a TableLayoutPanel
will allow for proper autosize behavtheitroad.
将 包裹FlowLayoutPanel
在 aTableLayoutPanel
中将允许适当的自动调整大小行为。
Your UserControl
should look like this:
你UserControl
应该是这样的:
UserControl
TableLayoutPanel (Dock-Fill)
Row1 : Label
Row2 : FlowLayoutPanel (Panel:Dock-Fill AND AutoSize, Row:AutoSize)
Row3 : Panel
Again, when using that UserControl
, it is possible you will need to wrap it in a TableLayoutPanel
using an AutoSize
row or column.
同样,在使用 that 时UserControl
,您可能需要TableLayoutPanel
使用AutoSize
行或列将其包装在 a 中。
Watch for SplitContainers
since they often throw autosize behaviors out of balance.
注意,SplitContainers
因为它们经常使 autosize 行为失去平衡。
回答by Bryan Menard
On adding a control to the flowlayout you can resize the already added controls to the required size without getting the scroll bars for flow layout panel.l
在向 flowlayout 添加控件时,您可以将已添加的控件的大小调整为所需的大小,而无需获取 flow layout panel.l 的滚动条