Label.AutoSize Property

Definition

Gets or sets a value indicating whether the control is automatically resized to display its entire contents.

public virtual bool AutoSize { get; set; }
[System.ComponentModel.Browsable(true)]
public override bool AutoSize { get; set; }

Property Value

true if the control adjusts its width to closely fit its contents; otherwise, false. When added to a form using the designer, the default value is true. When instantiated from code, the default value is false.

Attributes

Examples

The following code example demonstrates the AutoSize property. To run this example, paste the following code in a form and call the InitializeLabel method from the form's constructor or Load method.

// Declare a label.
internal System.Windows.Forms.Label Label1;

// Initialize the label.
private void InitializeLabel()
{
    this.Label1 = new Label();
    this.Label1.Location = new System.Drawing.Point(10, 10);
    this.Label1.Name = "Label1";
    this.Label1.TabIndex = 0;

    // Set the label to a small size, but set the AutoSize property 
    // to true. The label will adjust its length so all the text
    // is visible, however if the label is wider than the form,
    // the entire label will not be visible.
    this.Label1.Size = new System.Drawing.Size(10, 10);
    this.Controls.Add(this.Label1);
    this.Label1.AutoSize = true;
    this.Label1.Text = "The text in this label is longer" +  
        " than the set size.";
}

Remarks

When this property is set to true, the Label adjusts its width to display its entire contents. This property is typically set to true when you use a Label control to display various lengths of text, such as the status of an application process. You can also use this property when the application will display text in various languages, and the size of the text might increase or decrease based on the language settings in Windows.

Important

If the font is taller than the height of the Label and AutoEllipsis is true, you must set AutoSize to false for text to be drawn.

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also