Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement basic syntax highlighting in Winforms RichTextBox?

I have a list of words I want to highlight in my RichTextBox control, I have an idea on how to do it but having a problem with parsing everything to separate words.

How can I parse a line or the whole text into separate words and then enumerate over them and color them using RichTextBox.Select() method.

Is this a good way? Is there a better/faster way?

like image 805
Joan Venge Avatar asked May 12 '11 18:05

Joan Venge


People also ask

What is RichTextBox in C#?

In C#, RichTextBox control is a textbox which gives you rich text editing controls and advanced formatting features also includes a loading rich text format (RTF) files. Or in other words, RichTextBox controls allows you to display or edit flow content, including paragraphs, images, tables, etc.

How does syntax highlighter work?

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.


1 Answers

Use the RichTextBox.Find(String, Int32, Int32, RichTextBoxFinds) method to find your strings in the control. You can then iterate by changing the Start point to the point after the current word.

Not sure on the performance of this scheme but it will work.

http://msdn.microsoft.com/en-us/library/yab8wkhy.aspx

like image 158
Bueller Avatar answered Nov 15 '22 04:11

Bueller