Share via


TextBoxBase.AutoSize Properti

Definisi

Mendapatkan atau mengatur nilai yang menunjukkan apakah tinggi kontrol secara otomatis menyesuaikan ketika font yang ditetapkan ke kontrol diubah.

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

Nilai Properti

true jika tinggi kontrol secara otomatis menyesuaikan ketika font diubah; jika tidak, false. Default adalah true.

Atribut

Contoh

Contoh ini mengasumsikan bahwa Anda memiliki formulir dengan dua kotak teks, dua tombol, dan mengklik peristiwa untuk setiap tombol. Contoh menunjukkan AutoSize properti dengan mengaturnya ke true untuk satu kotak teks dan false untuk yang lain. Saat Anda mengklik satu tombol, kotak teks diisi dengan teks yang lebih kecil, dan saat Anda mengklik tombol lain, kotak teks diisi dengan teks yang lebih besar. Kotak teks yang telah AutoSize diatur untuk true memperluas tinggi untuk mengakomodasi teks yang lebih besar. Lebar tidak berubah.

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

Keterangan

Saat Anda mengatur AutoSize properti ke true untuk TextBox, saat Font berubah, TextBox akan diperluas atau dikontrak Height untuk mengakomodasi teks yang lebih besar atau lebih kecil. Width dari TextBox tidak berubah.

Jika Anda ingin mengubah ukuran kontrol saat pengguna memasukkan teks, Anda dapat menggunakan RichTextBox kontrol dan menggunakan peristiwanya ContentsResized untuk mengubah ukurannya.

Berlaku untuk