Akrem
Akrem

Reputation: 4652

a lot of information in richTextbox

I have a list with many line extracted from file and I want to display it a richTextbox with this code

foreach (string s in Dettaglio)
{
    txtDettaglio.Text += s + Environment.NewLine;
}

And Dettaglio definition is:

System.Collections.Generic.List<string> Dettaglio = new System.Collections.Generic.List<string>();

But it makes a lot of time to accomplish it there’s any other solution or I haven’t to use richTextbox?

Upvotes: 0

Views: 94

Answers (2)

Renatas M.
Renatas M.

Reputation: 11820

Maybe using StringBuilder will be faster

StringBuilder sb = new StringBuilder();
foreach (string s in Dettaglio)
{
    sb.Append(s + Environment.NewLine);
}
txtDettaglio.Text = sb.ToString();

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500765

Firstly: I'd use AppendText instead of string concatenation:

foreach (string s in Dettaglio)
{
    txtDettaglio.AppendText(s);
    txtDettaglio.AppendText(Environment.NewLine);
}

It may be faster to use concatenation to avoid calling AppendText twice:

foreach (string s in Dettaglio)
{
    txtDettaglio.AppendText(s + Environment.NewLine);
}

Now it could be that that won't actually be any faster, but it's what I'd try to start with - the internal data structure of RichTextBox may need to do work in order to fetch the Text property, and using AppendText you may avoid it having to reparse text that it's already handled.

Upvotes: 2

Related Questions