Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Label Class
Label Properties
 AutoSize Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Label..::.AutoSize Property

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

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Declaration)
<BrowsableAttribute(True)> _
Public Overrides Property AutoSize As Boolean
Visual Basic (Usage)
Dim instance As Label
Dim value As Boolean

value = instance.AutoSize

instance.AutoSize = value
C#
[BrowsableAttribute(true)]
public override bool AutoSize { get; set; }
Visual C++
[BrowsableAttribute(true)]
public:
virtual property bool AutoSize {
    bool get () override;
    void set (bool value) override;
}
JScript
public override function get AutoSize () : boolean
public override function set AutoSize (value : boolean)

Property Value

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

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 noteImportant Note:

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.

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.";

    }

Visual C++
   // Declare a label.
internal:
   System::Windows::Forms::Label ^ Label1;

private:

   // Initialize the label.
   void InitializeLabel()
   {
      this->Label1 = gcnew Label;
      this->Label1->Location = 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 = 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.";
   }

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Auto Sized Label control for the compact framework      samie   |   Edit   |   Show History
http://cf-sami.blogspot.com/2008/08/auto-sized-label-for-cf.html
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker