Assuming this is WinForms ..
I've worked with the TableLayoutPanel for years, and, I find that configuring it at run-time requires a certain series of steps. In your code, you are not creating the necessary ColumnStyle and RowStyle objects that determine the run-time presentation/structure.
You need to make choices about whether Row heights and Column widths are set to exact sizes, or set automatically, or set to percentages of the TableLayoutPanel's height and width.
Hopefully this example will get you going:
public void InitializeTLayoutPanel(TableLayoutPanel tbllp, int rowcount, int colcount)
{
tbllp.Controls.Clear();
tbllp.ColumnStyles.Clear();
tbllp.RowStyles.Clear();
tbllp.ColumnCount = colcount;
tbllp.RowCount = rowcount;
float collpcent = colcount / 100.0f;
float rowpcent = rowcount / 100.0f;
float colabswidth = 100.0f;
tbllp.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;
tbllp.AutoScroll = true;
tbllp.AutoSizeMode = AutoSizeMode.GrowAndShrink;
for (int col = 0; col <= tableLayoutPanel1.ColumnCount - 1; col++)
{
tbllp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, collpcent));
for (int rows = 0; rows <= tableLayoutPanel1.RowCount - 1; rows++)
{
tbllp.RowStyles.Add(new ColumnStyle(SizeType.Percent, rowpcent));
}
}
}
Note:
1) very unlikely anyone here will go to a sote that required creating an account to see a screenshot
2) in your code;
a) you create PictureBoxes and Labels, but, you don't keep a reference to them; you can know which PictureBox is clicked in the Click event, but, getting access to the Label ... like when you want to set its Text ...is going to require needless effort. Solution: keep references !
b) what's a LayoutItem ?