简体   繁体   English

C#:如何让用户控件正确自动调整大小

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

I have a UserControl which consists of a Label (Top), a FlowLayoutPanel (Fill, TopDown flow and no wrap) and a Panel (Bottom). 我有一个UserControl ,它由Label (Top), FlowLayoutPanel (Fill,TopDown流程和无包装)和Panel (Bottom)组成。 The user control creates a number of controls, based on a list of stuff it gets, and adds them to the FlowLayoutPanel . 用户控件根据获取的内容列表创建许多控件,并将它们添加到FlowLayoutPanel

How can I get this UserControl to properly resize itself so that the FlowLayoutPanel does not have any scroll bars? 如何让这个UserControl正确调整自身大小,以便FlowLayoutPanel没有任何滚动条? 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. 我试图在FlowLayoutPanelUserControl本身上使用AutoSizeAutoSizeMode各种组合,但我似乎无法让它工作。 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. 要么我最终得到的东西根本没有自我调整,要么它不会变得足够大或者它被压缩到几乎没有。

Thanks for all the suggestions. 感谢所有的建议。 The solution this time seemed to set AutoSize to true for both the FlowLayoutPanel and the UserControl itself. 这次解决方案似乎将FlowLayoutPanel UserControl本身的AutoSize设置为true

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的表单以及其他一些我目前无法理解的控件,但我想这应该是一个单独的问题......

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. 您可以使用UserControl的Anchor-和Dock属性来设置选项,以便控件的边缘“粘合”到UI的其他部分。 When the UI gets resized, your control will follow along! 当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。

Wrapping the FlowLayoutPanel in a TableLayoutPanel will allow for proper autosize behavoir. FlowLayoutPanel包装在TableLayoutPanel中将允许适当的自动调整行为。

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. 同样,在使用UserControl ,您可能需要使用AutoSize行或列将其包装在TableLayoutPanel

Watch for SplitContainers since they often throw autosize behaviors out of balance. 注意SplitContainers因为它们经常会使autosize行为失去平衡。

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. 并将usercontrol本身的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方法。

在向flowlayout添加控件时,您可以将已添加的控件的大小调整为所需的大小,而无需获取流布局panel.l的滚动条。

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

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