如何将UserControl停靠在FlowLayoutPanel中? [英] How do I dock a UserControl into a FlowLayoutPanel?

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

问题描述

我有一个FlowLayoutPanel和一个UserControl.

I have a FlowLayoutPanel and a UserControl.

我已经在FlowLayoutPanel中添加了多个用户控件,并且试图将它们停靠在顶部,因此,当我更改FlowLayoutPanel的大小时,用户控件的大小(宽度)也会相应地发生变化./p>

I've added multiple usercontrols into the FlowLayoutPanel, and I'm trying to dock them to the top, so when I change the size of the FlowLayoutPanel the size (width) of the usercontrols changes accordingly.

推荐答案

您不能将任何东西停放在FlowLayoutPanel中,它只会被忽略.

You cannot dock anything inside a FlowLayoutPanel, it's simply ignored.

查看在此处回答显然是由Microsoft团队发布的.

Check out the answer here apparently posted by the Microsoft team.

他们说:

FlowLayoutPanel依赖于最大的控件来有效地定义其中的列/行.下面的代码将第一个控件的大小设置为FLP的宽度,以实现与所需布局相似的布局.

The FlowLayoutPanel relies on a largest control to effectively define the column/row within it. The code below set's the size of the first control to the width of the FLP to achieve a layout similar to what you want.

    private void flowLayoutPanel1_Layout(object sender, LayoutEventArgs e)
    {
        flowLayoutPanel1.Controls[0].Dock = DockStyle.None;
        for (int i = 1; i < flowLayoutPanel1.Controls.Count; i++)
        {
            flowLayoutPanel1.Controls[i].Dock = DockStyle.Top;
        }
        flowLayoutPanel1.Controls[0].Width = flowLayoutPanel1.DisplayRectangle.Width - flowLayoutPanel1.Controls[0].Margin.Horizontal;

    }

关键是要使用Layout事件.

这个解决方案在某种程度上对我有用.您的UserControl必须关闭AutoSize/保持统一大小.

This solution worked for me up to a point. Your UserControls have to have AutoSize turned off / stay a uniform size.

在我的情况下,我希望打开AutoSize,以便允许UserControl在填充FlowLayoutPanel的宽度的同时垂直扩展/收缩.

In my case I wanted AutoSize turned on so as to allow the UserControl to expand/contract vertically while filling the width of the FlowLayoutPanel.

我必须找到其他解决方案.但是以上内容可能对您有帮助.

I had to find a different solution. But the above might help you in your case.

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

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