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

本範例假設您有一個表單,其中包含兩個文字方塊、兩個按鈕,以及每個按鈕的按一下事件。 此範例示範 AutoSize 屬性的方式是將它設定為 true 一個文字方塊,另 false 一個文字方塊則為 。 當您按一下一個按鈕時,文字方塊會填入較小的文字,當您按一下其他按鈕時,文字方塊會填入較大的文字。 設定 AutoSize 為以高度展開以 true 容納較大文字的文字方塊。 寬度不會變更。

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 ,以容納較大或較小的文字。 WidthTextBox 不會變更。

如果您想要在使用者輸入文字時變更控制項的大小,您可以使用 RichTextBox 控制項並使用其事件來變更其 ContentsResized 大小。

即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:https://aka.ms/ContentUserFeedback

提交並檢視相關的意見反應