C# 获取用户控件以填充其容器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1006941/
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 05:22:32  来源:igfitidea点击:

Getting a User Control to fill its container

c#winformssizeuser-controls

提问by Simon Morgan

I've created a user control which contains a label and a progress bar. I've also created another user control which contains only a flow layout panel with the flow direction set to top down. When I add the former to the flow layout panel of the latter, I would like it to use the maximum amount of horizontal space available and the minimum amount of vertical space such that the default heights of the label and progress bar are honoured. Is there a way to do this?

我创建了一个包含标签和进度条的用户控件。我还创建了另一个用户控件,其中仅包含一个流向设置为自上而下的流布局面板。当我将前者添加到后者的流布局面板时,我希望它使用最大可用的水平空间和最小的垂直空间,以便遵守标签和进度条的默认高度。有没有办法做到这一点?

I've uploaded a screenshotto help illustrate the problem. The user control containing the label and progress bar is in red and the user control containing the flow layout panel is in green.

我上传了一个屏幕截图来帮助说明问题。包含标签和进度条的用户控件为红色,包含流布局面板的用户控件为绿色。

回答by Bluestone

Just set the Dock property of the User Control to 'Top'...

只需将用户控件的 Dock 属性设置为“顶部”...

UserControl.Dock = System.Windows.Forms.DockStyle.Top;

回答by Zensar

Positioning and layering of controls can be tricky at times. Make sure the green flow control's Dock property is set to Fill. After that, start placing your label controls in and begin positioning. You can set the Dock property to Top if you want, but I am not a big fan of it in most situations. I'd say one of the most important things to remember in setting the position of controls, especially when using the Dock property, is that order matters. If you can't see a control (ie... it seems hidden behind other controls) then try reordering the way they are added to the parent (in this case the flow control panel). If you are using the designer in VS you can do this by right-click and using the "Send to top / bottom" commands (very useful when using the Dock property, furthermore you can also see which controls are layered in the spot you click).

控件的定位和分层有时会很棘手。确保绿色流控件的 Dock 属性设置为 Fill。之后,开始放置标签控件并开始定位。如果需要,您可以将 Dock 属性设置为 Top,但在大多数情况下,我并不喜欢它。我想说在设置控件位置时要记住的最重要的事情之一,尤其是在使用 Dock 属性时,顺序很重要。如果您看不到某个控件(即...它似乎隐藏在其他控件后面),请尝试重新排序它们添加到父控件的方式(在本例中为流控制面板)。如果您在 VS 中使用设计器,您可以通过右键单击并使用“发送到顶部/底部”命令(在使用 Dock 属性时非常有用,

Also, in my opinion an often overlooked option is the proper use of Control.Anchor. I would suggest looking at it.

此外,在我看来,一个经常被忽视的选项是正确使用Control.Anchor。我建议看一下。

回答by Ehz

I think the issue is because you don't actually want the controls to "Flow", you just want them to consume the top-most position in your host control. You can get the results you need by just using a regular Panel control instead. Just add the child user controls to the container control's Controls collection and set the added user control's Dock to "Top" afterwards.

我认为问题是因为您实际上并不希望控件“流动”,您只是希望它们占用主机控件中的最顶层位置。您只需使用常规面板控件即可获得所需的结果。只需将子用户控件添加到容器控件的 Controls 集合中,然后将添加的用户控件的 Dock 设置为“顶部”。

        ChildDisplay dsp1 = new ChildDisplay();
        pnlHost.Controls.Add(dsp1);
        dsp1.Dock = DockStyle.Top;

        ChildDisplay dsp2 = new ChildDisplay();
        pnlHost.Controls.Add(dsp2);
        dsp2.Dock = DockStyle.Top;

        ChildDisplay dsp3 = new ChildDisplay();
        pnlHost.Controls.Add(dsp3);
        dsp3.Dock = DockStyle.Top;

Note: Replace [ChildDisplay] with whatever is the name of the UserControl you are using.

注意:将 [ChildDisplay] 替换为您正在使用的 UserControl 的名称。