views:

32

answers:

1

I am in a situation where I want to add usercontrols in a panel. This has to be in a topdown order.

I have done whats explained here to make it act like a stackpanel. However I want the width of each usercontrol to fill out the panel and not be a fixed size.

I tried using tablelayoutpanel and setting it grows attribute to 'add rows' but since I might be in a situation with more space the height is sometimes not constant because it wants to fill out the last row.

+1  A: 

You can still use the TableLayoutPanel.

To add controls, you can use the TableLayoutPanel.Add(Control, int, int) to specify the control, row, and column to put it in (in your case, column would always be 0). You can add rows by modifying the TableLayoutPanel.RowCount property. You would then set the Control.Anchor property on each control you add so it will stretch to the TableLayoutPanel's width:

Button b = new Button();
b.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
tableLayoutPanel1.Add(b, 0, 0);

AnchorStyles.Top prevents it from stretching to the entire row's height.

This is just a rudimentary outline, but you should be able to work out how to make it work for you from this information, as well as the MSDN page for TableLayoutPanel.

nasufara
Thanks! It helped
bobjink