Here is the idea: I don't think using
RichTextBox
, and RTF in general would be really useful here. RTF format, being the standard, is quite ugly and hard to work with; importantly, it's majorly obsolete and rarely used these days for anything more or less complex. One other problem is that, traditionally, Microsoft API for RTF was never comprehensive and allows you much less than the format can actually carry (which you can easily see if you past some complex content in it: it will be rendered, but cannot be generated via the API). It looks like Microsoft never considered the RTF APIs seriously. Maybe, RTF in principle still can be used, hard to say…
Another thing is: I saw a number of syntax highlighting components, and
none of them used RTF. It also tells me something. People used very different approaches, so my
point is: if you want syntax highlighting, maybe you need to try to use something which other people use?
Here is what I would advise:
First syntax highlighting component I would point to comes as a part of well-known open-source IDE written in C# called SharpDevelop:
http://en.wikipedia.org/wiki/SharpDevelop[
^],
http://www.icsharpcode.net/OpenSource/SD/Default.aspx[
^].
The editor component itself is called AvalonEdit:
https://github.com/icsharpcode/SharpDevelop/wiki/AvalonEdit[
^].
This is the WPF component, so I would recommend using WPF. It might not be the option for you. (Did you tag what are you using right now? Yes, you did. Very good, and thank you.) In this case, you can dig into earlier versions of SharpDevelop written in
System.Windows.Forms
, where the similar component was also available and I remember it worked quite well.
In all cases, the components can work with wide spectrum of features you can describe as custom syntax.
Another text editing component with syntax highlighting I know is Scintilla:
http://en.wikipedia.org/wiki/Scintilla_%28editing_component%29[
^],
http://www.scintilla.org/[
^].
As it's written in C++, you would need a .NET wrapper to use it. Here is the available one:
http://scintillanet.codeplex.com/[
^].
I found syntax highlighting with Scintilla somewhat difficult, but maybe I just did not get into it well enough. :-)
Good luck,
—SA