Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a huge string to a textbox efficiently?

Tags:

string

c#

textbox

I have a massive string (we are talking 1696108 characters in length) which I have read very quickly from a text file. When I add it to my textbox (C#), it takes ages to do. A program like Notepad++ (unmanaged code, I know) can do it almost instantly although Notepad takes a long time also. How can I efficiently add this huge string and how does something like Notepad++ do it so quickly?

like image 702
User2400 Avatar asked Jan 19 '10 07:01

User2400


2 Answers

If this is Windows Forms I would suggest trying RichTextBox as a drop-in replacement for your TextBox. In the past I've found it to be much more efficient at handling large text. Also when making modifications in-place be sure to use the time-tested SelectionStart/SelectedText method instead of manipulating the Text property.

rtb.SelectionStart = rtb.TextLength;
rtb.SelectedText = "inserted text"; // faster

rtb.Text += "inserted text"; // slower
like image 69
Josh Avatar answered Oct 23 '22 17:10

Josh


Notepad and Window TextBox class is optimized for 64K text. You should use RichTextBox

like image 24
particle Avatar answered Oct 23 '22 18:10

particle