TextBoxBase.AutoSize Property

Definition

Gets or sets a value indicating whether the height of the control automatically adjusts when the font assigned to the control is changed.

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

Property Value

true if the height of the control automatically adjusts when the font is changed; otherwise, false. The default is true.

Attributes

Examples

This example assumes that you have a form with two text boxes, two buttons, and click events for each of the buttons. The 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. The text box that has AutoSize set to true expands in height to accommodate the larger text. The width does not change.

C#
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);
}

Remarks

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. The Width of the TextBox does not change.

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.

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