How to align controls in TableLayoutPanel ?

Published on Wednesday, December 22, 2010

There is no direct alignment property for TableLayoutPanel. So lots of programmer have similar question: How to align the controls in TableLayoutPanel ?

Finally I found several FAQs about Windows Form TableLayoutPanel control (Content from Microsoft site):

Q: How to stretch and align elements within a TableLayoutPanel ? **A: **Use the anchor property of the control to align to a particular side (e.g. Left will stick it to the Left side). Using opposing anchors, the control can be stretched. E.g. Anchor=Left|Right will stretch to fill the width. The Dock property works in a similar manner.

Q: How to absolutely position elements within a TableLayoutPanel ? A: The margin property of control can be used to adjust the distance from the edge of the cell. If you change the anchor to be Top|Left and change the Margin to be (20,3,0,0) the control will be 20 pixels from the left edge and 3 pixels from the top.

Additionally the tableLayoutPanel provides several methods to change the position of controls that are already added to the table: tableLayoutPanel.SetCellPosition(new TableLayoutPanelCellPosition(4,2)) NOTE: Via extender provider on the control itself the SetRow and SetColumn methods can be used.

To determine where a control was place programmatically use the following: public TableLayoutPanelCellPosition GetPositionFromControl(Control control);