Programmatically adding Label to Windows Form (Length of label?)

Wilson picture Wilson · Jul 19, 2012 · Viewed 46.8k times · Source

In my code, i create a label with the following:

Label namelabel = new Label();
namelabel.Location = new Point(13, 13);
namelabel.Text = name;
this.Controls.Add(namelabel);

The string called name is defined before this, and has a length of around 50 characters. However, only the first 15 are displayed in the label on my form. I tried messing with the MaximumSize of the label but to no avail.

Answer

LarsTech picture LarsTech · Jul 19, 2012

Try adding the AutoSize property:

namelabel.AutoSize = true;

When you place a label on a form with the design editor, this property defaults to true, but if you create the label in code like you did, the default is false.