guest86
guest86

Reputation: 2956

TableLayout problem with cell size (C#)

I have a TableLayoutPanel control that is initially empty - 0 rows and 0 cols. I want to form it, and fill it with some controls from code, and it works, but i have some nasty problem - when i add controls to TableLayoutPanel using code, cols and rows are not the same size (although all added controls ARE the same size, and docked to fill cells in TableLayoutPanel container). Basically first row takes about 50% of height, and two next rows take 25% each.

code i have tried looks like this:

//Reseting table:
tableLayoutPanel1.RowCount = 1;
tableLayoutPanel1.ColumnCount = 1;

//Size of one side of matrix:
int oneSide=(int)Math.Sqrt(mat.Matrix.Length);

tableLayoutPanel1.RowCount = oneSide;
tableLayoutPanel1.ColumnCount = oneSide;

tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 100 / oneSide));
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100 / oneSide));

//Matrix is an array of controls, mat is some class
tableLayoutPanel1.Controls.AddRange(mat.Matrix);

Above code isn't doing the job...How to add somtrols from code, and make all cells the same size? P.S. i HAVE to add controls from code, creating rows and cols in design-time is impossible.

Upvotes: 0

Views: 728

Answers (1)

Hans Passant
Hans Passant

Reputation: 941625

Doing this by hand is unpleasant, let a code generator do the job for you. Create a dummy form, drop a TLP on it. Get it the way you want it with the designer.

Next, locate the form in the Solution Explorer window and open the node to the left of it. Double-click the Designer.cs file. Locate the code in the InitializeComponent method and copy/paste it.

Upvotes: 1

Related Questions