Wilson Wilson - 18 days ago 6
C# Question

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

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

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.

Comments