簡體   English   中英

為什么在我的文本框中未更新Capacity屬性?

[英]Why does the Capacity property not update in my textbox?

Length屬性填充相應的textBox3 fine,並在從textBox1追加字符串時正確更新值。 textBox4僅讀取16,這是StringBuilder的初始值(默認),在第17個字符處不會更改為32(在第33個字符處也不會更改為64)。

為什么Capacity屬性不能繼續填充/更新textBox4?

public partial class StringBuilderExercise : Form
{
    public StringBuilderExercise()
    {
        InitializeComponent();
    }
    StringBuilder buffer = new StringBuilder();
    // input textbox
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
    }
    // button appends textBox1 string to textBox2
    private void append_Click(object sender, EventArgs e)
    {
        textBox2.AppendText(textBox1.Text);
        textBox3.Text = textBox2.Text.Length.ToString();
        textBox4.Text = buffer.Capacity.ToString();
        textBox1.Clear();
    }
    //read-only textbox to display string append concat
    private void textBox2_TextChanged(object sender, EventArgs e)
    {
    }
    //read-only textbox to display length of string
    private void textBox3_TextChanged(object sender, EventArgs e)
    {          
    }
    //read-only textbox to display capacity of string buffer
    private void textBox4_TextChanged(object sender, EventArgs e)
    {   
    }
}

原因是您永遠不會將任何數據追加到StringBuilder。

我想你的意思是這樣的:

buffer.Append(textBox1.Text);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM