The TableLayoutPanel control displays the controls that it contains in rows and columns. This makes it easy to build grids of regularly spaced controls.
Figure G-26 shows a form that uses a TableLayoutPanel running on top of the IDE displaying the same form. The TableLayoutPanel control displays prompts and data-entry controls in two columns. The Employee label at the top is set to span both of the control’s columns. The Form Designer in the background shows the control’s rows and columns using dashed lines. In this example, the control doesn’t draw its grid lines at runtime.
Figure G-26: In this example, the TableLayoutPanel control displays two columns of controls arranged in a grid.
The following table describes the TableLayoutPanel control’s most useful properties.
Property | Purpose |
---|---|
AutoScroll | Determines whether the control automatically provides scroll bars if the controls it contains won’t fit. |
CellBorderStyle | Determines the cell border style. This can be None, NotSet (an appropriate style is selected based on the row and column styles), Inset (single sunken line), InsetDouble (double sunken line), Outset (single raised line), OutsetDouble (double raised line), OutsetPartial (single line containing a raised area), and Single (single line). |
ColumnCount | Determines the number of columns. |
ColumnStyles | A collection giving column styles. |
ColumnWidths | An array of column widths. |
Controls | A collection of controls contained within the control. |
Enabled | Determines whether the control is enabled. If the TableLayoutPanel is disabled, then the controls it contains are also disabled. |
GrowStyle | Determines how the control grows when you add new child controls to it. This can be AddRows, AddColumns, or FixedSize (the control throws an exception if you add more controls). |
RowCount | Determines the number of rows. |
RowHeights | An array of row heights. |
RowStyles | A collection of row styles. |
Visible | Determines whether the control and its contents are visible. |
The following table describes the TableLayoutPanel control’s most useful methods.
Method | Purpose |
---|---|
GetColumn | Returns a child control’s column number. |
GetColumnSpan | Returns the number of columns that a child control spans. |
GetRow | Returns a child control’s row number. |
GetRowSpan | Returns the number of rows that a child control spans. |
ScrollControlIntoView | If the TableLayoutPanel control has AutoScroll set to True, then this scrolls an indicated child control into view. |
SetColumn | Sets a child control’s column number. |
SetColumnSpan | Sets a child control’s column span. |
SetRow | Sets a child control’s row number. |
SetRowSpan | Sets a child control’s row span. |
In addition to providing its own properties, the TableLayoutPanel acts as a property provider for its child controls. These properties include Column, ColumnSpan, Row, and RowSpan. For example, if you add a button to the TableLayoutPanel control named TableLayoutPanel1, then the button’s Properties window will contain an entry labeled Column on TableLayoutPanel1 that determines the button’s column.
The TableLayoutPanel control also changes the meaning of its child controls’ Anchor property. By default, a child control has Anchor property set to None, so it is centered in its table cell. If you set Anchor to Left, then the control is moved to the left edge of the cell. If you set Anchor to Left, Right, then both of the control’s edges are attached to the cell’s edges, so the control is stretched to fit the cell’s width. The Top and Bottom Anchor settings work similarly.
The FlowLayoutPanel control also arranges contained controls, but not in a grid. It places controls one after another to fill either rows or columns. For information on that control, see the section “FlowLayoutPanel” earlier in this appendix.