views:

47

answers:

1

I need a lean & mean TextBox solution. A RichTextBox proves too slow, so I want to go the way of owner drawing, or custom control building.

My need is for a textbox that can handle large text content and offers simple highlighting by drawing colored backgrounds around words or single characters. Important is, that the text string itself does NOT contain markup for this, but rather, the indices of words to mark are stored separately. Indices relative to the start of the text string (also known as the Text property when talking about a .NET TextBox).

I think it will have to involve drawing the text under my own control as the Windows Edit Control will not be able to do what I need.

My application is Windows Forms. What is the proper way to make a control like this, and are there any examples?

And can one make a fast control under .NET? (already assuming native API calls will be needed). Or is this better done in C++?

+2  A: 

You cannot realistically implement your own TextBox, writing your own text editor is punishing. Use something off the shelf, like ScintillaNET.

Writing your own scrollable Label is somewhat do-able. Start with a double-buffered Panel, use OnPaint to draw the text. It is only ever going to be an improvement if you don't implement word wrap, that makes it very expensive to figure out where to start drawing since you have to wrap all the text before the scrolled first visible line. Calculating the AutoScrollMinSize is expensive since you have to scan the entire text to count the line breaks, make sure you don't have to update the Text too often.

In general, the odds are good that you'll merely find out why TextBox is as slow as it is. You keep it performant by limiting the amount of text to what a human can reasonably be expected to ever want to read through. Which isn't much. I do it by keeping track of the length of the Text for each append and throwing half of it away when it goes past 65536 characters. A nice round number.

Hans Passant
Thanks for your answer. I'm not replying yet to the content as I'm busy on something else for a period of time but I will elaborate on this later...
Kay Zed
Take your time, this answer isn't going anywhere. Of course, you can always unmark it later.
Hans Passant