Coloring words from Richtextbox

Asked

Viewed 425 times

4

My goal is to color all the words that are inside double quotes, and the quotation marks as well!

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

  • Take a look at this project here: https://github.com/PavelTorgashov/FastColoredTextBox

1 answer

1


You can use the Regex to select the double-quote string and then add

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;

            }
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.