Follow steps to use TableLayoutPanel in Windows Forms to position our controls more precisely. We require an attractive and functional GUI that resizes properly when users change the size of the window. Some controls, such as a TextBox, may need to be aligned to the bottom or right of spaces.
Here we use .NET 3.5, but really this applies to many different configurations. First, to make a TableLayoutPanel, expand the toolbox on the left side. Double click on the TableLayoutPanel icon in the left, and then open the Properties pane on the right.
You should see a four-pane panel now, which you can position controls within. You will need to modify the number of columns and rows, and then you can also change row spans and column spans. Here are some properties that are available when using TableLayoutPanel.
| Property | Usage |
| Anchor | Used to align, expand, or fill a control--more below |
| Column | Used to specify number of columns--use for making new cells on the right |
| Row | Specifies number of rows--add 1 to this to get a new row on the bottom |
| GrowStyle | I haven't actually used this--could be useful for when you need even more flexibility |
When you click on the space next to Anchor in the Property pane on the right, you will see a box that looks like a +. Click on the rectangles that make up the + and anchor the control that is within the table panel cell to that edge. Here are those steps again.
The TextBox you made will now expand to fill the entire width. To create a multi-line TextBox that fills the entire cell, first set the TextBox as multiline, and then add Top and Bottom in the Anchor menu by clicking on the up and down parts of the +. Here are some more things you can do with the TableLayoutPanel.
| If you need to | Then you should |
| align a control to any side of a cell | set the appropriate Anchor property in the +, but only that direction |
| expand the control to the full cell | set all directions on the Anchor + |
| center a control in a cell | set no directions in the Anchor + |
TableLayoutPanel may solve almost all of the control alignment problems you have. TableLayoutPanel is quite flexible and can greatly enhance your program's UI, but it requires a planning step before dragging controls to your form. The cost of TableLayoutPanel is far less than the cost of having to individually adjust each control's layout.
The absolute worst thing you can do is make the layout rigid. Having a rigid layout makes life hard for you, as the designer, and the user who is trying to use the program in a slightly different configuration. Eliminate tedious pixel adjustments and use TableLayoutPanel.