Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Syntax Highlight in a RichTextBox [C#]?

How do I syntax highlight in a richtextbox control AS THE USER TYPES and USING A String[] keywords. I will be publishing a lightweight notepad to the web soon and I want it to have syntax highlighting. I am using Windows forms. Can someone post a code example?

like image 983
Mohit Deshpande Avatar asked Nov 08 '09 17:11

Mohit Deshpande


People also ask

Does Visual Studio have syntax highlighting?

Syntax highlighting determines the color and style of source code displayed in the Visual Studio Code editor. It is responsible for colorizing keywords like if or for in JavaScript differently than strings and comments and variable names.

What is syntax highlighting text editor?

Syntax highlighting is a feature of text editors that are used for programming, scripting, or markup languages, such as HTML. The feature displays text, especially source code, in different colours and fonts according to the category of terms.


6 Answers

  • RichTextBox syntax highlighting (talks about RichTextBox itself - minimal features but exactly what you asked for here)
  • A textbox/richtextbox that has syntax highlighting? [C#] (talks mostly about other ways of doing it)
like image 134
Sam Harwell Avatar answered Oct 13 '22 13:10

Sam Harwell


The Scintilla control is an excellent source code editor that includes syntax highlighting amongst a whole range of other features. You can embed it in your own app and there is a .NET wrapper available.

With Scintilla you can specify the keywords and it will then apply the syntax highlighting as you type.

like image 29
Matt Warren Avatar answered Oct 13 '22 12:10

Matt Warren


Are you using WinForms or WPF?

If WPF, you could have a look at AvalonEdit. It's free and open source, and it's used in SharpDevelop (open source IDE).

like image 26
Winston Smith Avatar answered Oct 13 '22 13:10

Winston Smith


You can change the font of selected words in the richtextbox. Take a look at the Select and SelectedFont properties of the control.

But essentially, you need to iterate through the words, check if a word is present in your keywords, and then change the font, using the above-mentioned properties.

like image 41
Jan Avatar answered Oct 13 '22 13:10

Jan


Not exactly an answer to your question, but have you looked at the text editor component from SharpDevelop? It's quite lightweight (<200kB IIRC), can be easily embedded in WinForms applications and has syntax highlighting for several languages built in.

Otherwise, you might want to look at this CodeProject page. It reformats the RTF while you type, which is not very efficient for large files, and it contains a few creepy catch (Exception) { } blocks, so I'm not sure if I would use it in a life-critical application, but it's definitely a good starting point to see how it can be done.

like image 39
Niki Avatar answered Oct 13 '22 13:10

Niki


Syntax highlighting is not an easy task to perform efficiently. Many solutions you can find (like the ones involving modification of RTF) are a one time solution. If you want to highlight and un-highlight words on the fly during edition, your code has to be ready for it. I would not reinvent the wheel and use ICSharp.TextEditor or alike to solve your problem.

like image 24
jdehaan Avatar answered Oct 13 '22 11:10

jdehaan