Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 107,669 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,045 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Syntax Highlight in C#

 
Reply to this topicStart new topic

> Syntax Highlight in C#, How to implement syntax highlighting in a WinForms application.

PixelCard
Group Icon



post 9 Jul, 2008 - 09:43 AM
Post #1


In this tutorial you will see how to implement the syntax highlighting feature in a C# WinForms application. For this purpose I will use the RegularExpressions class, member of the System.Text namespace.

Special Tutorial Requirements:
  • C# IDE (Visual Studio 2008 used in this tutorial)
  • .NET Framework 1.0

So, here we go.

1. Create a C# Windows Forms application:

IPB Image

2. Add a RichTextBox control to the form:

IPB Image

3. Add a Timer control to the form:

IPB Image

4. Change the following timer properties:
  • Enabled = True
  • Interval = 1000

5. Add the System.Text.RegularExpressions namespace to the project:

csharp

using System.Text.RegularExpressions;


6. Create a new regex right after "public partial class Form1 : Form {". I will name it keyWords:[b]

csharp

public Regex keyWords = new Regex& quot;abstract|as|base|bool|break|byte|case|catch|char|checked|class|const|contin
ue|decimal|default|delegate|do|double|else|enum|event|explicit|extern|false|fina
lly|fixed|float|for|" +
& quot;foreach|goto|if|implicit|in|int|interface|internal|is|lock|long|namespace|n
ew|null|object|operator|out|override|params|private|protected|public|readonly|re
f|return|sbyte|sealed|short|sizeof|stackalloc|static|" + & quot;string|struct|switch|this|throw|true|try|typeof|uint|ulong|unchecked|unsafe
|ushort|using|virtual|volatile|void|while|");


My regex contains all C# reserved words from this MSDN page:
C# Keywords

[b]7. This code goes for the timer (timer1_Tick):


csharp

private void timer1_Tick(object sender, EventArgs e)
{
//Highlight every found word from keyWords.

//Get the last cursor position in the richTextBox1.

int selPos = richTextBox1.SelectionStart;

//For each match from the regex, highlight the word.
foreach (Match keyWordMatch in keyWords.Matches(richTextBox1.Text))
{

richTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length);
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.SelectionStart = selPos;
richTextBox1.SelectionColor = Color.Black;
}
}


Of course you can add more regex variables for different words and different colors for highlighting.

Every 1 second the text will be parsed for matches and every found match will be highlighted. This code can be modified. Instead of using the timer's tick event the richTextBox's TextChanged event can be used. The code will look like this:

csharp

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
//Highlight every found word from keyWords.

//Get the last cursor position in the richTextBox1.

int selPos = richTextBox1.SelectionStart;

//For each match from the regex, highlight the word.
foreach (Match keyWordMatch in keyWords.Matches(richTextBox1.Text))
{

richTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length);
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.SelectionStart = selPos;
richTextBox1.SelectionColor = Color.Black;
}
}




Attached File(s)
Attached File  syntaxHighlight.zip ( 39.31k ) Number of downloads: 46
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 8/29/08 10:58PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month