1

Hi I just started a little hobby project.

The goal is to write a very simple dev environment including an editor with simple syntax highlighting. I did tackle this years before as a rookie VB coder with little understanding what I was acutally doing. So I found my old project (batch file editor) and went through the code. What I did back then was looping trough the RTB Text and looking for keywords. When I had found one I set the cursor poistion to the word select it and use the SelectionColor property to change the color then reset the cursor to its previous location.

This solution seemed pretty messy to me at first. And it obviously is. For example if you want to be able to process files and not limit the size you need to relieve stress and the algorithm which searches through the text. Doing this by using another thread may cause the cursor to jump to unwanted locations while the user is typing which could cause a definitely unwanted effect... So I went online searching for other solutions but as it seems there are pretty much no other approaches out.

So I thought before starting to rewrite a complete own TextBox supporting this I might ask here if anyone knows a good solution to this. Preferably without using any libraries not included into .Net but I'm starting to take this into consideration.

Robin B
  • 1,066
  • 1
  • 14
  • 32
  • It is not messy but the recommended way to use a RTF. Or you could create raw Rtf code. (Talking about real messy..) – TaW Aug 08 '15 at 20:30
  • Just tried the selection approach again... cursor jumps all over the place... but thanks for this rtf tipp found a nice article [here](http://dotnet-csharp-programming.blogspot.de/2010/04/xml-highlighting-in-rich-textbox.html) which im gonna look into now – Robin B Aug 08 '15 at 21:02
  • You can stop the layout while setting the colors.. (SuspendLayout) – TaW Aug 08 '15 at 21:11
  • well depending on the documents' size this really could add a lot of stuttering and/or unresponsiveness :/ – Robin B Aug 08 '15 at 21:51
  • Do have a look at [Hans answer to the problem here](http://stackoverflow.com/questions/3282384/richtextbox-syntax-highlighting-in-real-time-disabling-the-repaint?rq=1) – TaW Aug 08 '15 at 21:54
  • Altough this seems like an interesting approach to workaround alot of problems its still not perfect when it comes to larger file sizes.The solution would cause any false inputs problems etc but it still wouldnt redraw for a considerable time. This would feel like coding over teamviewer on a 56k connection :/. Altough much thanks for all your advises but I think I will take the before considered approach and use an already out there control. I had much success by using [this one](http://www.codeproject.com/Articles/161871/Fast-Colored-TextBox-for-syntax-highlighting) so far. – Robin B Aug 08 '15 at 22:55
  • Don't reinvent the wheel; it's very hard (if not impossible) to create a decent syntax highlighter using RichTextBox. Use an existing control like ICSharpCode.TextEditor – Thomas Levesque Aug 09 '15 at 01:17

0 Answers0