1

I'm working on a control, which contains a TableLayoutPanel. The panel contains X rows and Y columns - where each cell is the same size. Each cell contains a Control When the parent controls is being resized, I want each cell to be resized as well, and keep the same size to each other.

I've tried to set the Controls' Anchor when added to the cell:

control.Anchor = AnchorStyles.Left | AnchorStyles.Top |
                 AnchorStyles.Right | AnchorStyles.Bottom;

But this only makes the bottom row and the right most column expand.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Moelbeck
  • 591
  • 2
  • 7
  • 29

1 Answers1

2

Using designer or code, you can set SizeType of the columns and rows to Percent and assign same percentage value for their size. Also set Dock property of your controls to Fill.

For example:

tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.RowStyles.Clear();
for (var i = 0; i < tableLayoutPanel1.ColumnCount; i++)
{
    var percent = 100f / (float)tableLayoutPanel1.ColumnCount;
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, percent));
}
for (var i = 0; i < tableLayoutPanel1.RowCount; i++)
{
    var percent = 100f / (float)tableLayoutPanel1.RowCount;
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, percent));
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thanks! this was a great help. But there is something odd going on: The last row, and the last column is just a tiny bit bigger than the other rows / columns? if I set the SizeType to Absolute, they will be the same size, but will not resize. – Moelbeck Sep 18 '17 at 08:06
  • You're welcome. Take a look at an example [here](https://stackoverflow.com/a/33969228/3110834). I couldn't reproduce the problem which you mentioned in comments. – Reza Aghaei Sep 18 '17 at 08:11