获取用户控件来填充其容器

发布于 2024-07-24 00:48:38 字数 274 浏览 3 评论 0原文

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

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

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 screenshot to 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莳間冲淡了誓言ζ 2024-07-31 00:48:38

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

        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;

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

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.

        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.

千纸鹤带着心事 2024-07-31 00:48:38

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

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

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

UserControl.Dock = System.Windows.Forms.DockStyle.Top;
征棹 2024-07-31 00:48:38

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

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

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).

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文