tags:

views:

1231

answers:

3

In a WPF application, I want to build a "Find in Files" output pane, in which I can stream large quantity of text, without re-allocating memory at each line, like the TextBox would do.

The WPF TextBox has a single Text property which stores a contiguous string. Each time, I want to add content, I need to do textBox.Text += "New Text", which is bad.

Ideally, that control would be virtual and require a minimum of resources, just for the visible lines.

I thought about using a standard ListBox with a VirtualizingStackPanel, but it does not allow Text Selection across lines.

(At each new line added, I want the control to update)

Any suggestion?

A: 

Have you considered or tried the RichTextBox control?

Drew Noakes
We have tried, but the performance become disastrous, probably because of the overkill related to string formatting, which I don't need.
decasteljau
Thanks for letting me know. @codymanix's answer sounds perfect -- I didn't know that.
Drew Noakes
A: 

A StringBuilder, just append the text to the String builder and instead of doing

textBox.Text += moreText;

do

myStringBuilder.Append(moreText);
textBox.Text = myStringBuilder.ToString();

This should take care of the Schlemiel the Painter's algorithm.

Of course, the string builder should have to be a member of your class so it exists through your object's life span.

Carlo
each time you call ToString() on the StringBuilder, it allocates a new contiguous string containing the concatenated strings. Since I will be appending new lines to the control all the time, I exactly don't want that.For each new line, I want the control to update.
decasteljau
+2  A: 

If you do not expect much more than ten-thousands of search results in your application, a TextBlock control or readonly multiline TextBox will suffice by far.

The TextBox class has an AppendText() method which should be fast enough for you.

If you need text highlighting / formatting then maybe you want to use RichTextBox.

codymanix
Thanks codymanix, the MSDN doc says:The AppendText method enables the user to append text to the contents of a text control without using text concatenation, which, can yield better performance when many concatenations are required.Also after looking at the implementation inside .NET Reflector, the AppendText is exactly what I need.
decasteljau
decasteljau
Keep in mind that the textbox could be storing each of your appends for undo operations. You might want to change the UndoLimit field on the textbox.
Kelly