Click here to Skip to main content
13,089,172 members (53,998 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 31 Aug 2010

code highlight syntax

, 31 Aug 2010
Rate this:
Please Sign up or sign in to vote.
small example to show how to used Regular Expressions for code highlight

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:[^]

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;



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Moustafa Farhat
Software Developer (Senior) syrian developers
Syrian Arab Republic Syrian Arab Republic
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...


Comments and Discussions

Generalhere link to download: Pin
ck47ht20-Apr-11 4:32
memberck47ht20-Apr-11 4:32 
GeneralReason for my vote of 1 Simply awful. Don't try this at home... Pin
Toli Cuturicu2-Sep-10 9:03
memberToli Cuturicu2-Sep-10 9:03 
GeneralReason for my vote of 1 Tons of error and don't really give ... Pin
Simon Dufour31-Aug-10 5:44
memberSimon Dufour31-Aug-10 5:44 
GeneralI guess it's not a bad start but prehaps you should also che... Pin
Simon Dufour31-Aug-10 4:01
memberSimon Dufour31-Aug-10 4:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 31 Aug 2010
Article Copyright 2010 by Moustafa Farhat
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid