将控件停靠在停靠的 FlowLayoutPanel 中
Docking a Control inside a Docked FlowLayoutPanel
我们将 属性 "Dock = Dockstyle.Fill" 添加到我们的 FlowLayoutPanel 中,以便它调整大小并填充其父控件。现在我们向 FlowLayoutPanel 添加了两个 GroupBox。这些应该与 Panel 具有相同的宽度,但是当我们使用 "Dock = Dockstyle.Top" 时它不起作用。
问题是,我们试图用 "Width = Parent.Width" 设置宽度。这会起作用,但使用我们的方法,通过 XML 文件创建 UI,目前我们想要设置宽度,GroupBoxes 还没有父级。稍后将添加到 FlowLayoutPanel 中。
顺便说一下,我们还向 FlowLayoutPanel 添加了 "FlowDirection = TopDown",但是如果 GroupBoxes 变小,它会将它们并排放置,而不是 TopDown。
所以我们正在寻找一种方法,使所有控件都位于彼此下方,并使所有 GroupBox 的宽度与 FlowLayoutPanel 的宽度相同。
感谢您的帮助,
多米尼克
在您描述的情况下,当您只需要自上而下的流程时,您可以简单地使用 Panel 而不是 FlowLayoutPanel。将panelAutoScroll
设置为true,其Dock
设置为Fill
,然后将group boxes添加到panel,其中Dock
属性设置为Top
.
注:
对于 FlowLayoutPanel
的未来使用,您可能会发现这很有帮助:
How to: Anchor and Dock Child Controls in a FlowLayoutPanel
Control
This is the general rule for anchoring and docking in the FlowLayoutPanel control:
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 (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
您可以通过取消评论代码来颠倒项目的顺序。
我们将 属性 "Dock = Dockstyle.Fill" 添加到我们的 FlowLayoutPanel 中,以便它调整大小并填充其父控件。现在我们向 FlowLayoutPanel 添加了两个 GroupBox。这些应该与 Panel 具有相同的宽度,但是当我们使用 "Dock = Dockstyle.Top" 时它不起作用。
问题是,我们试图用 "Width = Parent.Width" 设置宽度。这会起作用,但使用我们的方法,通过 XML 文件创建 UI,目前我们想要设置宽度,GroupBoxes 还没有父级。稍后将添加到 FlowLayoutPanel 中。
顺便说一下,我们还向 FlowLayoutPanel 添加了 "FlowDirection = TopDown",但是如果 GroupBoxes 变小,它会将它们并排放置,而不是 TopDown。
所以我们正在寻找一种方法,使所有控件都位于彼此下方,并使所有 GroupBox 的宽度与 FlowLayoutPanel 的宽度相同。
感谢您的帮助,
多米尼克
在您描述的情况下,当您只需要自上而下的流程时,您可以简单地使用 Panel 而不是 FlowLayoutPanel。将panelAutoScroll
设置为true,其Dock
设置为Fill
,然后将group boxes添加到panel,其中Dock
属性设置为Top
.
注:
对于 FlowLayoutPanel
的未来使用,您可能会发现这很有帮助:
How to: Anchor and Dock Child Controls in a FlowLayoutPanel Control
This is the general rule for anchoring and docking in the FlowLayoutPanel control:
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 (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
您可以通过取消评论代码来颠倒项目的顺序。