SIGN UP MEMBER LOGIN:    
ARTICLE

Syntax Highlighting in RichTextBox using C#

Posted by Kirtan Patel Articles | Visual C# August 03, 2010
This article is about how to achieve Syntax Highlight feature in RichTextBox.
Reader Level:
Download Files:
 

When we make an application like programming editor for language it provides syntax highlighting that makes the user comfortable to work with it as everybody likes colored text that can be easy to read and distinguish between normal variable names and language keywords.

In this I will show you how provide such syntax highlighting feature in RichTextBox control.

It's simple to make it.

Drag a RichTextBox control on the form.. Now we need when user types text it should be highlighted if its keyword of language. I will use Blue color here to highlight the keyword of language.

Now to make this feature available to user we need a collection of all keywords of language. So here in demonstration, I'm making syntax highlighting for C language so I need C language Keyword list...

I acquired all from here


Now think about how we should update the color of text obviously it would be nice if we update color of text to blue as we type in RichTextBox. So we will write code in RichTextBox's text changed event.

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;
    }
}

You can see here in first line I have written all the keywords of C language that we want to highlight. Then I match collection and color them one by one by selecting it and change its font color.

And here is my result 

1.gif

Login to add your contents and source code to this article
share this article :
post comment
 

nice post

Posted by sanabil ramzan Mar 24, 2012

werwrewrw

Posted by Binod Wosti Sep 29, 2010

What if you have a public void called public then it highlights the public.

Posted by Larry Rock Aug 25, 2010

This code works well with small files.  But beyond a certain size, it becomes unusable.  The entire RTB gets updated with every keypress - creating significant visual artifacts and delays.  Do you have any suggestions for improving this?

Thanks.

Posted by Imad Mansour Aug 18, 2010

nice post, it's very ease, but what if the text will have 1000 words or more? 

Sorry for ma bad English. 

Posted by Ihor Bats Aug 18, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor