65.9K
CodeProject is changing. Read more.
Home

code highlight syntax

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.75/5 (4 votes)

Aug 31, 2010

CPOL
viewsIcon

15532

small example to show how to used Regular Expressions for code highlight

Introduction this is small example to show how to used Regular Expressions for code highlight Using the code you can download the example or used this code here link to download: http://dev-sy.com/wp-content/plugins/download-monitor/download.php?id=4[^] if you want to use the code in the first you should add richtext to the form
 using System.Text.RegularExpressions;
private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
              string tokens = "(auto|double|int|struct|break|else|long|switch|case|enum|register|typedef|char|extern|return|union|const|float|short|unsigned|continue|for|signed|void|default|goto|sizeof|volatile|do|if|static|while)";
    Regex rex = new Regex(tokens);
    MatchCollection mc = rex.Matches(richTextBox1.Text);
    int StartCursorPosition = richTextBox1.SelectionStart;
    foreach (Match m in mc)
    {
        int startIndex = m.Index;
        int StopIndex = m.Length;
        richTextBox1.Select(startIndex, StopIndex);
        richTextBox1.SelectionColor = Color.Blue;
        richTextBox1.SelectionStart = StartCursorPosition;
        richTextBox1.SelectionColor = Color.Black;
    }
        }