Coloring RichTextBox words

3

My goal is to color all words that are within double quotation marks, and the quotation marks too!

For example: "This text needs to be colored".

    
asked by anonymous 02.07.2016 / 22:14

1 answer

0

You can use Regex to select double-quoted strings and then colorize them

Ex:

private void ColorirRichTextBox()
{

    string[] linhas = richTextBox1.Lines;
    foreach (var item in linhas)
    {
        System.Text.RegularExpressions.MatchCollection matchs = System.Text.RegularExpressions.Regex.Matches(item, @"""[^""\]*(?:\.[^""\]*)*""");
        foreach (System.Text.RegularExpressions.Match match in matchs)
        {

            string mystring = match.Value;

            if (richTextBox1.Find(mystring) > 0)
            {

                int my1stPosition = richTextBox1.Find(mystring);

                richTextBox1.SelectionStart = my1stPosition;

                richTextBox1.SelectionLength = mystring.Length;


                richTextBox1.SelectionColor = Color.Red;

            }
        }
    }
}
    
02.07.2016 / 23:08