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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:34:47  来源:igfitidea点击:

C#: How to get a user control to properly auto size itself

c#winformsdynamicuser-controls

提问by Svish

I have a UserControlwhich 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.

我有一个UserControlLabel(顶部)、一个FlowLayoutPanel(填充、自上而下流和无包装)和一个Panel(底部)组成的。用户控件根据它获取的内容列表创建许多控件,并将它们添加到FlowLayoutPanel.

How can I get this UserControlto properly resize itself so that the FlowLayoutPaneldoes not have any scroll bars? I have tried to use various combinations of AutoSizeand AutoSizeModeon the FlowLayoutPaneland the UserControlitself, 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,并AutoSizeModeFlowLayoutPanelUserControl本身,但我似乎无法得到它的工作。要么我最终得到的东西根本不会调整自己的大小,要么它变得不够大,要么被压扁成几乎没有。

采纳答案by Svish

Thanks for all the suggestions. The solution this time seemed to set AutoSizeto truefor both the FlowLayoutPanelandthe UserControlitself.

感谢所有的建议。该解决方案这段时间似乎设置AutoSizetrue两者的FlowLayoutPanelUserControl本身。

Now, how to get the form which will contain this UserControlas 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 FlowLayoutPanelin a TableLayoutPanelwill allow for proper autosize behavtheitroad.

将 包裹FlowLayoutPanel在 aTableLayoutPanel中将允许适当的自动调整大小行为。

Your UserControlshould 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 TableLayoutPanelusing an AutoSizerow or column.

同样,在使用 that 时UserControl,您可能需要TableLayoutPanel使用AutoSize行或列将其包装在 a 中。

Watch for SplitContainerssince 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 的滚动条