You could try this:
public partial class Form1 : Form
{
List<TableLayoutCellConfig> tlcc = new List<TableLayoutCellConfig>();
public Form1()
{
InitializeComponent();
tlcc.Add(new TableLayoutCellConfig(0, 0, Brushes.Green));
tlcc.Add(new TableLayoutCellConfig(0, 1, Brushes.Red));
tlcc.Add(new TableLayoutCellConfig(1, 0, Brushes.Lime));
}
private void TableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
foreach (TableLayoutCellConfig conf in tlcc)
{
if (e.Row == conf.Row && e.Column==conf.Column)
{
e.Graphics.FillRectangle(conf.Brush, e.CellBounds);
continue;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
tlcc.Add(new TableLayoutCellConfig(1, 1, Brushes.Pink));
tableLayoutPanel1.Invalidate();
}
}
class TableLayoutCellConfig
{
public TableLayoutCellConfig(int X, int Y, Brush Background)
{
this.Column = X;
this.Row = Y;
this.Brush = Background;
}
private int row;
public int Row
{
get { return row; }
set { row = value; }
}
private int column;
public int Column
{
get { return column; }
set { column = value; }
}
private Brush brush;
public Brush Brush
{
get { return brush; }
set { brush = value; }
}
}
With
List<TableLayoutCellConfig>
you can iterate through your config during
TableLayoutPanel1_CellPaint
Don't forget to invalidate after changing config.