12

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.

Microsoft DN
  • 9,706
  • 10
  • 51
  • 71
user3256753
  • 145
  • 1
  • 1
  • 6
  • @MicrosoftDN : winform. – user3256753 Feb 24 '14 at 06:43
  • 2
    Seems like you want some sort of syntax highlight, so check out this post: [A textbox/richtextbox that has syntax highlighting?](http://stackoverflow.com/questions/1087735/a-textbox-richtextbox-that-has-syntax-highlighting-c) – scheien Feb 24 '14 at 06:49
  • See the answer in this post [Color different parts of a RichTextBox string](http://stackoverflow.com/questions/1926264/color-different-parts-of-a-richtextbox-string) – scheien Feb 24 '14 at 06:51
  • Possible duplicate of [Color different parts of a RichTextBox string](https://stackoverflow.com/questions/1926264/color-different-parts-of-a-richtextbox-string) – Knowledge Cube Jun 01 '17 at 17:45

4 Answers4

25

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;
            }
        }
    }
roadrunner66
  • 7,772
  • 4
  • 32
  • 38
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
6

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;
            };
        }
    }
Jpsh
  • 1,697
  • 12
  • 17
2

You can use richTextBox1_KeyDown event

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                String abc = this.richTextBox1.Text.Split(' ').Last();
                if (abc == "while")
                {
                    int stIndex = 0;
                    stIndex = richTextBox1.Find(abc, stIndex, RichTextBoxFinds.MatchCase);
                    richTextBox1.Select(stIndex, abc.Length);
                    richTextBox1.SelectionColor = Color.Aqua;
                    richTextBox1.Select(richTextBox1.TextLength, 0);
                    richTextBox1.SelectionColor = richTextBox1.ForeColor;
                }
            }
        }
Microsoft DN
  • 9,706
  • 10
  • 51
  • 71
1

If instead of a word you want to highlight a regular expression, you can use this:

   private void ColorizePattern(string pattern, Color color)
   {
            int selectStart = this.textBoxSrc.SelectionStart;                
            foreach (Match match in Regex.Matches(textBoxSrc.Text, pattern))
            {
                textBoxSrc.Select(match.Index, match.Length);
                textBoxSrc.SelectionColor = color;
                textBoxSrc.Select(selectStart, 0);
                textBoxSrc.SelectionColor = textBoxSrc.ForeColor;
            };      
   }
Ahmad
  • 8,811
  • 11
  • 76
  • 141