Label.AutoSize Property

Definition

获取或设置一个值,该值指示是否自动调整控件的大小以显示其完整内容。Gets or sets a value indicating whether the control is automatically resized to display its entire contents.

public:
 virtual property bool AutoSize { bool get(); void set(bool value); };
[System.ComponentModel.Browsable(true)]
public override bool AutoSize { get; set; }
public virtual bool AutoSize { get; set; }
member this.AutoSize : bool with get, set
Public Overrides Property AutoSize As Boolean
Public Overridable Property AutoSize As Boolean

Property Value

Boolean

如果控件调整宽度以完整显示内容,值为 true;反之,值为 falsetrue if the control adjusts its width to closely fit its contents; otherwise, false.


使用设计器添加到窗体时,默认值为 trueWhen added to a form using the designer, the default value is true. 当从代码实例化时,默认值为 falseWhen instantiated from code, the default value is false.

Attributes

Examples

下面的代码示例演示了 AutoSize 的属性。The following code example demonstrates the AutoSize property. 若要运行此示例,请将以下代码粘贴到窗体中,并从窗体的构造函数或 Load 方法中调用 InitializeLabel 方法。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;

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.";
   }
// 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.";
}
' 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

Remarks

如果将此属性设置为 trueLabel 将调整其宽度以显示其完整内容。When this property is set to true, the Label adjusts its width to display its entire contents. 当你使用 Label 控件显示各种文本长度(例如应用程序进程的状态)时,此属性通常设置为 trueThis 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. 当应用程序将以各种语言显示文本时,还可以使用此属性,并且根据 Windows 中的语言设置,文本大小可能会增加或减少。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

如果字体超出 Label 的高度,并且 AutoEllipsis true,则必须将 AutoSize 设置为要绘制的文本 falseIf 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

See also