TextBoxBase.AutoSize 属性

定义

获取或设置一个值,该值指示当更改分配给控件的字体时,是否自动调整控件的高度。Gets or sets a value indicating whether the height of the control automatically adjusts when the font assigned to the control is changed.

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

属性值

Boolean

如果在更改字体时自动调整控件的高度,则为 true;否则为 falsetrue if the height of the control automatically adjusts when the font is changed; otherwise, false. 默认值为 trueThe default is true.

属性

示例

此示例假设你有一个窗体,其中包含两个文本框,两个按钮,然后单击每个按钮的 "事件"。This example assumes that you have a form with two text boxes, two buttons, and click events for each of the buttons. 该示例演示 AutoSize 属性,方法是将其设置为,将其设置为,并将其设置为 true falseThe example demonstrates the AutoSize property by setting it to true for one text box and false for the other. 单击一个按钮时,文本框将用较小的文本进行填充,单击另一个按钮时,文本框将用较大的文本进行填充。When you click one button the text boxes are filled with a smaller text, and when you click the other button the text boxes are filled with larger text. AutoSize 设置为 true 容纳较大文本的文本框。The text box that has AutoSize set to true expands in height to accommodate the larger text. 不会更改宽度。The width does not change.

private void button1_Click(object sender, EventArgs e)
{
    this.textBox1.AutoSize = true;
    this.textBox1.Text = "Hello world!";
    this.textBox1.Font = new System.Drawing.Font("Arial", 10, FontStyle.Regular);

    this.textBox2.AutoSize = false;
    this.textBox2.Text = "Hello world!";
    this.textBox2.Font = new System.Drawing.Font("Arial", 10, FontStyle.Regular);
}

private void button2_Click(object sender, EventArgs e)
{
    this.textBox1.AutoSize = true;
    this.textBox1.Text = "Goodbye world!";
    this.textBox1.Font = new System.Drawing.Font("ArialBlack", 14, FontStyle.Regular);

    this.textBox2.AutoSize = false;
    this.textBox2.Text = "Goodbye world!";
    this.textBox2.Font = new System.Drawing.Font("ArialBlack", 14, FontStyle.Regular);
}
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Me.TextBox1.AutoSize = True
    Me.TextBox1.Text = "Hello world!"
    Me.TextBox1.Font = New System.Drawing.Font("Arial", 10, FontStyle.Regular)

    Me.TextBox2.AutoSize = False
    Me.TextBox2.Text = "Hello world!"
    Me.TextBox2.Font = New System.Drawing.Font("Arial", 10, FontStyle.Regular)
End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Me.TextBox1.AutoSize = True
        Me.TextBox1.Text = "Goodbye world!"
    Me.TextBox1.Font = New System.Drawing.Font("ArialBlack", 14, FontStyle.Regular)

    Me.TextBox2.AutoSize = False
        Me.TextBox2.Text = "Goodbye world!"
    Me.TextBox2.Font = New System.Drawing.Font("ArialBlack", 14, FontStyle.Regular)
End Sub

注解

如果将的 AutoSize 属性设置为,则在更改时,将 true TextBox Font TextBox 展开或收缩 Height 以容纳更大或更小的文本。When you set the AutoSize property to true for a TextBox, when the Font changes, the TextBox expands or contracts the Height to accommodate the larger or smaller text. Width TextBox 不会更改。The Width of the TextBox does not change.

如果要在用户输入文本时更改控件的大小,则可以使用 RichTextBox 控件并使用其 ContentsResized 事件更改其大小。If you want to change the size of the control as the user enters text, you can use a RichTextBox control and use its ContentsResized event to change its size.

适用于