Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Label.AutoSize Property

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

[Visual Basic]
Public Overridable Property AutoSize As Boolean
[C#]
public virtual bool AutoSize {get; set;}
[C++]
public: __property virtual bool get_AutoSize();
public: __property virtual void set_AutoSize(bool);
[JScript]
public function get AutoSize() : Boolean;
public function set AutoSize(Boolean);

Property Value

true if the control adjusts its width to closely fit its contents; otherwise, false. The default is false.

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.

Example

[Visual Basic, C#] 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.

[Visual Basic] 
' Declare a label.
Friend WithEvents Label1 As System.Windows.Forms.Label

' Initialize the label.
Private Sub InitializeLabel()
    Me.Label1 = New Label
    Me.Label1.Location = New System.Drawing.Point(10, 10)
    Me.Label1.Name = "Label1"
    Me.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.
    Me.Label1.Size = New System.Drawing.Size(10, 10)
    Me.Controls.Add(Me.Label1)
    Me.Label1.AutoSize = True
    Me.Label1.Text = "The text in this label is longer than the set size."

End Sub

[C#] 
// 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.";

}

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Label Class | Label Members | System.Windows.Forms Namespace | Text | PreferredHeight | PreferredWidth

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft