0

So I want to use the Text.Replace function to replace non-colored text with colored text, but that non-colored text has to be a specific word. eg.

        private void timer1_Tick(object sender, EventArgs e)
        {
            int index = 0;
            while(index < richTextBox1.Text.LastIndexOf(textBox1.Text))
            {
                richTextBox1.Find("if");
                richTextBox1.Text.Replace("if", Text.Color.DarkGreen "if");
        }
    }
}
TaW
  • 53,122
  • 8
  • 69
  • 111
Jake F.
  • 141
  • 1
  • 3
  • 17
  • Are you trying to implement syntax highlighting? There are controls out there which supports it. Working with plain RichTextBox for that is a real pain. – Sriram Sakthivel Mar 05 '16 at 17:16
  • @SriramSakthivel Yes, I am trying to implement syntax highlighting, but not all of it is syntax – Jake F. Mar 05 '16 at 17:20
  • 1
    Keep in mind that the Replace function returns the new string. – Mathijs Mar 05 '16 at 17:26
  • I recommend you to use any of third party textboxes which supports syntax highlighting. Most of them provide custom highlighting using xml files which define the syntax highlighting. One of them is [here](https://syntaxhighlightbox.codeplex.com/) search in your favorite search engine with keyword "C# Syntax Highlighting Wpf/Winforms/Asp.net" whichever technology you use – Sriram Sakthivel Mar 05 '16 at 17:44
  • What are you targetting: Winforms? WPF? ASP? ...?? __Always__ tag your question accordingly! - Winforms: This cannot work. Nor only do you use Replace wrong you also __never must change the Text property__ of a RichTextBox or you will mess up all previous formatting. Instead you __need__ to select a portion and then change the SelectedColor!!! – TaW Mar 05 '16 at 18:17
  • @TaW the problem with that is i would have to manually select it and i want my program to be lightweight so adding 50 hours of code won't help – Jake F. Mar 15 '16 at 23:13
  • Um, no. A selection is not made manually but by setting `SelectionStart` and `SelectionLength`. – TaW Mar 16 '16 at 06:40

1 Answers1

0

The coloring is not so hard but defining the term 'word' is not so simple..

Here is an example for both:

I assume that you can live with the Regex definition of word boundaries. You may need to improve on the wordPattern depending on your needs, i.e. on your grammar! (See here for an example!)

int ColorWord(RichTextBox rtb, string word, Color foreColor, Color backColor)
{
    string wordPattern  = @"\b" + word +  @"\b";
    var matches = Regex.Matches(rtb.Text, wordPattern);

    for (int i = matches.Count - 1; i >= 0; i--)
    {
        var m = matches[i];

        rtb.SelectionStart = m.Index;
        rtb.SelectionLength = m.Length;
        rtb.SelectionColor = foreColor;
        rtb.SelectionBackColor = backColor;
    }

    return matches.Count;
}

This function will set fore- and backcolor for each occurance of a given word in a RichTextBox. It transverses the matches from the back just for good measure as we don't really modify any text, so the letters won't shift; so we could have looped from start to end as well; but maybe you want to adapt it one day for changing words..

Once you understand how coloring (or formatting in any other way) the RTB text works, i.e. by first selecting a portion of the text and then changing the RTB.SelectedXXX property, it should be simple to modify..

Note that the word boundaries are meant to work with normal text. It is up to you to define maybe new rules to include or exclude characters for the language you want to highlight..

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111