Hasan
Hasan

Reputation: 335

How do I dock a UserControl into a FlowLayoutPanel?

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.

Upvotes: 1

Views: 2420

Answers (1)

hawbsl
hawbsl

Reputation: 16043

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.

Upvotes: 3

Related Questions