Sponja Sponja - 2 months ago 6
C# Question

Why doesn't this Label matrix display correctly?

I'm trying to make a label grid in Windows Forms, but only one of the columns gets displayed:

rows = columns = 20;
letters = new Label[rows, columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
letters[i, j] = new Label();
letters[i, j].Parent = this;
letters[i, j].Name = i + "_" + j;
letters[i, j].TextAlign = ContentAlignment.MiddleCenter;
letters[i, j].Location = new Point(20 + 20 * i, 20 + 20 * j);
letters[i, j].Visible = true;
letters[i, j].Text = "A";
}
}


enter image description here

Answer

You forgot to assign a Size property, so the labels just contain a default size (with big Width) that overlaps the others.

// ...
letters[i, j].Size = new Size(20, 20);
// ...