How to select text from RichTextBox and then its color?

I want to create a simple editor, for example Notepad ++ with simple functionality ... I need to color a specific word in a rich area text box. How can i do this?

For example: when a user writes this word, I want to color them blue. These words: for , while , if , try , etc.

How can I get richtextbox to select a specific word and then color it? And, if I want to make a comment and color everything after // , how is this done in richtextbox?

How can I specify a line in a text box, so now I can specify the line number when I code in my editor?

+5
c #
source share
5 answers

Here is what code you can use to achieve your desired functionality.

 private void ColourRrbText(RichTextBox rtb) { Regex regExp = new Regex("\b(For|Next|If|Then)\b"); foreach (Match match in regExp.Matches(rtb.Text)) { rtb.Select(match.Index, match.Length); rtb.SelectionColor = Color.Blue; } } 

CodeProject article Enabling syntax highlighting in a RichTextBox shows how to use RegEx in a RichTextBox to highlight syntax. In particular, look at SyntaxRichtTextBox.cs for an implementation.

+19
source share

In general, you need to work on your selection in RichTextBox. You can manipulate the current selection using the Find method or using the SelectionStart and SelectionLength properties. You can then change the properties of the selected text using the SelectionXXX properties. For example, SelectionColor set the color of the current selection, etc. Thus, you need to analyze the text in richtextbox, and then select a part of the texts and change their properties to suit your requirements.

Writing a good text editor using RichTextBox can be quite cumbersome. For this you should use some library like Scintilla . Check out ScintillaNet , the .NET wrapper on top of Scintilla.

+4
source share

Do you know that Notepad ++ uses Scintilla ?

In fact, you don’t need to reinvent the wheel while experiencing all the problems, as there is a Scintilla .NET port called ScintillaNET , which you can freely insert into your application as a source code editor :)

But to answer your question, there are several parts that you need to understand.

  • Color search
  • When color
  • Like color

  • There may be different approaches for the first part, but I think using regular expressions would be a good choice. I'm sorry, but I do not know regular expressions, so I can not help you in this case.

  • When color is very important, and if you do it wrong, your application will be seriously affected by performance degradation. I suggest you turn to XPath Visualizer , which was made by our own member of Stack Overflow, Cheeso. See the source for how syntax coloring was done. But if you are ScintillaNET, everything will be taken care of. In any case, I really cannot find this documentation, where it clearly showed how the coloring of the text was done. I would definitely post it here if I find it.

  • The third question, in my opinion, is covered by VinayC. But basically you use SelectionColor along with SelectionStart .

+2
source share

here is a good link on c-sharpcorner.com for the main syntax highlighting richtextbox. I assume that you and anyone who visits this page for a similar problem want to do this for training. But if someone wants to do this for some creating a commercial-level IDE, then he should use scintilla or something like that. Another approach is to directly modify the RTF for richtextbox . Look at codeproject.com, there are many articles similar to this problem.

0
source share

I had some problems with this, and here is my solution, it beats me why it needs to be done this way, but it works:

  // position on end of control... richTextBox.UpdateLayout(); richTextBox.ScrollToEnd(); richTextBox.UpdateLayout(); // ...then select text and it will be position on top of control. richTextBox.Focus(); richTextBox.Selection.Select(foundRange.Start, foundRange.End); richTextBox.BringIntoView(); 
0
source share

All Articles