Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winforms: FlowLayoutPanel with Docking

This is in winforms. I am creating a User control that is basically a FlowlayoutControl filled with other User Controls. I need each of the controls added to be docked to the top of the previous (from left to right). Unfortunately it looks like the flowlayoutcontrol ignores any of the docking properties. Is there any way to dock the controls inside there? I need it to fill the item from left to right, but the items should be laid out like a list view. Theres really no code i can provide due to the fact that its a matter of figuring out what approach to take.

like image 620
Alex Avatar asked Jun 22 '11 14:06

Alex


People also ask

How is anchoring different from docking?

Anchor refers to the position a control has relative to the edges of the form. A textbox, for example, that is anchored to the left edge of a form will stay in the same position as the form is resized. Docking refers to how much space you want the control to take up on the form.

What is Dock in Windows form?

Remarks. Use the Dock property to define how a control is automatically resized as its parent control is resized. For example, setting Dock to DockStyle. Left causes the control to align itself with the left edges of its parent control and to resize as the parent control is resized.


1 Answers

Getting the FlowLayoutPanel to dock right is tricky. From the original question, you want something like a list view. It's important to know that ONE of the items in your list (the widest one) defines a "virtual column" in the FlowLayoutPanel. The rest of the items will follow it. You can prove this in the VS designer by dragging one of the items to the right. The 'virtual column' will follow it, and if your other items are anchored they will follow the virtual column.

Note that you can't anchor the control that is defining the column. It has nothing to anchor to and strange things will happen.

Do do all this programatically, handle the Layout event on your FlowLayoutPanel and put code similar to the code below. It's important that in the designer all the items in your list are not docked and and have their anchoring set to 'none'. I spent a day on this yesterday and doing that in the designer is what made the code below work.

flowLayoutPanel.Controls[0].Dock = DockStyle.None;                
flowLayoutPanel.Controls[0].Width = flowLayoutPanel.DisplayRectangle.Width - lowLayoutPanel.Controls[0].Margin.Horizontal;

for (int i = 1; i < flowLayoutPanel.Controls.Count; i++)
{
    flowLayoutPanel.Controls[i].Dock = DockStyle.Top;
} 
like image 90
DustinA Avatar answered Sep 18 '22 11:09

DustinA