How do I dock a UserControl into a FlowLayoutPanel?

北慕城南 提交于 2019-12-11 08:29:47

问题


I have a FlowLayoutPanel and a UserControl.

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.


回答1:


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

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

They say:

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;

    }

Key thing is to use the Layout event.

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

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.



来源:https://stackoverflow.com/questions/7880593/how-do-i-dock-a-usercontrol-into-a-flowlayoutpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!