Bob Goblin
Bob Goblin

Reputation: 1269

TableLayoutPanel Spacing Issue

I adding labels to a tablelayoutpanel from an array. The labels add no problem, but their is a huge gap between each of the labels. Did I code something incorrectly? This is my array, and adding the labels. The tablelayoutpanel is added from the GUI and is named tablelayoutpanel1

while (dr.Read())
{
labelsToAdd.Add(dr[0].ToString());
}
dr.Close();
foreach (string label in labelsToAdd)
{
Label lbl = new Label();
lbl.Name = "lbl_" + index;
lbl.Text = label;
lbl.AutoSize = true;
tableLayoutPanel1.Controls.Add(lbl, 0, rowIndex);
rowIndex++;
}

So they will add like this with all the whitespace in between them

label1





label2

Visual Sample - enter image description here

Upvotes: 0

Views: 605

Answers (2)

LarsTech
LarsTech

Reputation: 81610

From your comment:

only the 1st two display like that. The rest display as desired.

So it looks like you should clear the rows first since your GrowStyle = AddRows:

tableLayoutPanel1.RowStyles.Clear();
foreach (string label in labelsToAdd)
{
  Label lbl = new Label();
  // etc...

Upvotes: 3

MikeG
MikeG

Reputation: 545

Try setting the row height:

tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 30))

Upvotes: 0

Related Questions