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

查看:80
本文介绍了将控件停靠在停靠的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",但是如果GroupBox变得更小,则将它们并排放置而不是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 控制

How to: Anchor and Dock Child Controls in a FlowLayoutPanel Control

这是在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天全站免登陆