Speed ​​up adding text to a TextBox

I have code to set TexBox text as

 textBox1.Text = s;

where s is a string with more than 100,000 char, and it takes a long time to display text in a text field.

Does anyone have a solution to make this faster?

+4
source share
2 answers

To do this, split the string sinto many lines and use AppendText to add these substrings, if you check the MSDN you will see that:

AppendText , , .

 public string s = "Put you terribly long string here";

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //For responsiveness 
        textBox1.BeginInvoke(new Action(() =>
        {
            //Here your logic
            for (int i = 0; i < s.Length; i += 1000)
            {
                //This if is just for security
                if (i+1000 > s.Length)
                {
                    //Here your AppendText
                    textBox1.AppendText(s.Substring(i, s.Length-i));
                }
                else
                {
                    //And it here as well
                    textBox1.AppendText(s.Substring(i, 1000));
                }
            }
        }));
    }

1000, 1500, 2000, , . , .

:

AppendText WindowsForms, WPF, WindowsPhone WinRT. , .

+2

break s , , , .

for(int i=0;i<s.length; i++)
{
   textBox1.Text += s[i];
 }

0

Source: https://habr.com/ru/post/1526326/


All Articles