在停靠的 FlowLayoutPanel 内停靠控件 [英] Docking a Control inside a Docked FlowLayoutPanel

查看:24
本文介绍了在停靠的 FlowLayoutPanel 内停靠控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们向 FlowLayoutPanel 添加了属性Dock = Dockstyle.Fill",以便它调整大小并填充其父控件.现在我们向 FlowLayoutPanel 添加了两个 GroupBox.这些应该与面板具有相同的宽度,但是当我们使用Dock = Dockstyle.Top"时它不起作用.

We added the property "Dock = Dockstyle.Fill" to our FlowLayoutPanel so that it resizes and fills its parent control. Now we added two GroupBoxes to the FlowLayoutPanel. These should have the same width as the Panel, but when we use "Dock = Dockstyle.Top" it doesn't work.

问题是,我们试图用Width = Parent.Width"设置宽度.这会起作用,但使用我们的方法,通过 XML 文件创建 UI,在我们想要设置宽度的那一刻,GroupBoxes 还没有父级.稍后将添加到 FlowLayoutPanel 中.

Problem is, we tried to set the width with "Width = Parent.Width". That would work but with our approach, to create the UI via a XML File, at the moment we want to set the width, the GroupBoxes doesn't have a parent yet. It will be added to the FlowLayoutPanel later.

顺便说一下,我们还在 FlowLayoutPanel 中添加了FlowDirection = TopDown",但是如果 GroupBoxes 变小,它会将它们并排放置而不是 TopDown.

By the way we also added the "FlowDirection = TopDown" to the FlowLayoutPanel, but if the GroupBoxes become smaller it places them side by side instead of TopDown.

因此,我们正在寻找一种方法来让所有控件位于彼此下方,并使所有 GroupBox 的宽度与 FlowLayoutPanel 的宽度相同.

So we are looking for a way to get all controls beneath each other and to get all GroupBoxes the same width as the FlowLayoutPanel.

感谢大家的帮助,

多米尼克

推荐答案

在你描述的情况下,当你只想要自顶向下的流程时,你可以简单地使用 Panel 而不是 FlowLayoutPanel.将面板AutoScroll设置为true,将其Dock设置为Fill,然后将分组框添加到面板并设置Dock将它们的属性设置为 Top.

In the case that you described, when you only want top-down flow, you can simply use Panel instead of FlowLayoutPanel. Set the panel AutoScroll to true, and its Dock to Fill and then add group boxes to panel and set Dock property of them to Top.

注意:
对于 FlowLayoutPanel 的未来使用,您可能会发现这很有帮助:

Note:
For future uses of FlowLayoutPanel you may find this helpful:

如何:锚定和停靠子控件一个 FlowLayoutPanel控制

这是 FlowLayoutPanel 控件中锚定和停靠的一般规则:

This is the general rule for anchoring and docking in the FlowLayoutPanel control:

对于垂直流向,FlowLayoutPanel 控件计算来自最宽子控件的隐含列的宽度柱子.此列中所有其他带有 Anchor 或 Dock 的控件属性被对齐或拉伸以适合这个隐含的列.这对于水平流动方向,行为以类似的方式工作.这FlowLayoutPanel 控件从以下位置计算隐含行的高度行中最高的子控件,以及所有停靠或锚定的子控件此行中的控件对齐或调整大小以适合隐含的行.

For vertical flow directions, the FlowLayoutPanel control calculates the width of an implied column from the widest child control in the column. All other controls in this column with Anchor or Dock properties are aligned or stretched to fit this implied column. The behavior works in a similar way for horizontal flow directions. The FlowLayoutPanel control calculates the height of an implied row from the tallest child control in the row, and all docked or anchored child controls in this row are aligned or sized to fit the implied row.

示例:

例如,如果您运行以下代码:

For example following, if you run following code:

for (int i = 0; i < 5; i++)
{
    var control = new GroupBox()
    {
        Text = i.ToString(),
        Dock = DockStyle.Top,
        Height = 40
    };

    this.panel1.Controls.Add(control);
    //To reverse the order, uncomment following line
    //control.BringToFront();
}

结果将是:

4
3
2 
1
0

您可以通过取消注释代码来颠倒项目的顺序.

You can reverse the order of items, by uncommenting the comment code.

这篇关于在停靠的 FlowLayoutPanel 内停靠控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆