Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color specific words in RichtextBox

Tags:

c#

winforms

How can I make, that when the user types specific words like 'while' or 'if' in a rich textbox, the words will color purple without any problems? I've tried diffrent codes, but none of them were usable. The codes were like below:

if (richTextBox.Text.Contains("while"))
  {
    richTextBox.Select(richTextBox.Text.IndexOf("while"), "while".Length);
    richTextBox.SelectionColor = Color.Aqua;
  }

Also, I want that when the user removes the word or removes a letter from the word, the word will color back to its default color. I'm making a program with a code editor in it.

I am using Visual c#.

Thank you.

like image 955
user3256753 Avatar asked Feb 24 '14 06:02

user3256753


People also ask

How to set colored text in RichTextBox?

There is a property to set colored text by using SelectionText. Richtextbox has the properties of SelectionFont, SelectionColor and SelectedText. Setting the values for these properties makes multiple colors in our richtextbox.

How to test multiple colored texts in textbox?

There are two buttons available to test. One is for red colored text and the other is for green colored text. You can change the colors as per your wish by changing the codebehind. The following code snippet explains to us how to do the multiple colored texts in textbox.

How to change the color of text in a code snippet?

I have given the code snippet with this article. You can download the attached sample application and try to see the magic by clicking the buttons. There are two buttons available to test. One is for red colored text and the other is for green colored text. You can change the colors as per your wish by changing the codebehind.

What is the use of color text in chat?

This is basically used for making some chat application. We will have to show the text of the person at the other end in a different color in the chat application, so that we can use this method.


2 Answers

Add an event to your rich box text changed,

  private void Rchtxt_TextChanged(object sender, EventArgs e)
        {
            this.CheckKeyword("while", Color.Purple, 0);
            this.CheckKeyword("if", Color.Green, 0);
        }



private void CheckKeyword(string word, Color color, int startIndex)
    {
        if (this.Rchtxt.Text.Contains(word))
        {
            int index = -1;
            int selectStart = this.Rchtxt.SelectionStart;

            while ((index = this.Rchtxt.Text.IndexOf(word, (index + 1))) != -1)
            {
                this.Rchtxt.Select((index + startIndex), word.Length);
                this.Rchtxt.SelectionColor = color;
                this.Rchtxt.Select(selectStart, 0);
                this.Rchtxt.SelectionColor = Color.Black;
            }
        }
    }
like image 183
Sajeetharan Avatar answered Oct 16 '22 05:10

Sajeetharan


This is something that you can do, I would recommend using a regular expression to find all matches of the word in case it occurs many times. Also keep in mind that the string find could be a list of strings out side of a for loop to consider multiple words but this should get you started.

//dont foget to use this at the top of the page
using System.Text.RegularExpressions;

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        string find = "while";
        if (richTextBox1.Text.Contains(find))
        {
            var matchString = Regex.Escape(find);
            foreach (Match match in Regex.Matches(richTextBox1.Text, matchString))
            {
            richTextBox1.Select(match.Index, find.Length);
            richTextBox1.SelectionColor = Color.Aqua;
            richTextBox1.Select(richTextBox1.TextLength, 0);
            richTextBox1.SelectionColor = richTextBox1.ForeColor;
            };
        }
    }
like image 31
Jpsh Avatar answered Oct 16 '22 05:10

Jpsh