[.net] RichTextBox and syntax highlighting

Started by
5 comments, last by cignox1 16 years, 1 month ago
Hi, I'm trying to implement a user control which inherit from a RichTextBox and implement various options, such as syntax highlighting. For this part, I don't really know how to do it. At first, I tried to override the OnTextChanged method, and do my stuff as the user type text. Problem occurs when pasting text. And it rapidely became very complex to handle new highlighting rules :s Then I thought I could use regular expression. But the problem is that I have to use them on the whole text, and I have the feeling it would be slow. Any suggestion ? Or if you already used Regex with C#, is it fast enough so that it wouldn't be a problem for texts a 100's lines ? Thx.
Advertisement
Using a rich text box for syntax highlighting is a fairly large exercise in pain. Consider using an existing syntax highlighting control like scintilla.
Yes, I already noticed that it's not really simple :)
And I also did find a few existing controls (not the one you linked though :))
But that doesn't really answer my question. Doing this is 50% for personal knowledge / 50% for work. So if it's feasible with regular expressions, I'd like to give it a try.
I just need to know if regular expression with C# are fast enough to handle a few 100s of lines :)
It depends on how often you use the regex, how complicated the regex is and how powerful the computer running the code is. Basically, the only way to know for sure is to try it out and see if it'll work.
I'm trying to obtain something similar for our game interface, the main difference being I have to render everything using a DX based engine; it turned out to be a rather complex problem as you've already noticed.

For starters, you could have a look at SharpDevelop and its code. I think there's a wealth of informations about creating a formattable textbox from scratch. There's a lot of things to take in account, I wouldn't be able to summarize them in a post, so I suggest to try and get a copy of the SharpDevelop book.

Hope this helps.

Rainweaver Framework (working title)

IronLua (looking for a DLR expert)



http://www.dotnetfireball.net/

That has an open source Syntax Highlighting editor.
There are a few articles on this topic on http://www.codeproject.com but as already said, working with RichTextBox can become quite hard if you want to add functionalities. I began to wrote a simple text editor but if I wanted to start that project again, I would get an opensource editor next time :-)

This topic is closed to new replies.

Advertisement