Setting the space between cells in TableLayoutPanel


Jan 3, 2018
Programming Experience
I've got a TableLayoutPanel in which I create rows of the following "on the fly":

Label TextBox TextBox
Label TextBox TextBox
Label TextBox TextBox

I create the controls like this:

for (int i = 0; i < Properties.Settings.Default.rowcount; i++) {
    userPanel.Controls.Add(new Label() { Text = "Login" });
    userPanel.Controls.Add(new TextBox());
    userPanel.Controls.Add(new TextBox());        }

From the Designer.cs file:

this.userPanel.AutoSize = true;
this.userPanel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.userPanel.ColumnCount = 3;
this.userPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 60F));
this.userPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 165F));
this.userPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 165F));
this.userPanel.Location = new System.Drawing.Point(12, 153);
this.userPanel.Margin = new System.Windows.Forms.Padding(0, 0, 0, 35);
this.userPanel.Name = "userPanel";
this.userPanel.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.userPanel.RowCount = 3;
this.userPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.userPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.userPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.userPanel.Size = new System.Drawing.Size(390, 0);
this.userPanel.TabIndex = 6;

I might have a couple of misunderstanding etc. with regards to the sizing, size types etc.

The problem is that the space between the cells is too big, and I'm running out of space when I want to increase the size of the TextBoxes.

So: Is there a way to set the spacing between the controls/cells in the TableLayoutPanel? Since I'm not creating them by creating objects and naming them, am I missing out on something? The reason I'm creating them like this is that I have methods for adding and removing rows in the layout.
There is no space between the cells. If you want your child controls to fill their cells then you can set their Dock property to Fill. Note that any space between child controls then will be determined only by their Margin properties. If the Margin of each child control is all zeroes then there will be no space between child controls, other than accounting for the fact that the Height of a single-line TextBox is determined by the Font.

That said, if your controls are always going to be used in groups of three then I would suggest that you actually create a user control that contains those three controls. You can then simply add a single instance of that user control to get all three as a unit, thus meaning that you only need one column in your TLP. Mind you, if you want the columns to size proportionally then multiple columns may still be beneficial. To be honest, I'd tend to use a TLP with three columns and one row inside the user control for that though. If those three child controls are supposed to represent a unit then they should be designed as a unit and a user control does that.
Thanks. Good advice and clarification :tennis:

Anchor seems to work as well. And I agree with what you're saying about one control containing the cells.
Top Bottom