Joe
Joe

Reputation: 538

Highlight Syntax in RichTextBox using C#

How would I be able to highlight syntax in a RichTextBox using C#, like it is done in an IDE?

If so, Would I be able to do something like this, or is this over-complicated?

public String SyntaxHighlight(string ToHighlight)
{
     string Highlighted = null;
     List<string> Blue = new List<string>();
     Blue.Add("public");
     Blue.Add("private");
     Blue.Add("static");
     Blue.Add("string");

     //And so on...

     for(int i = 0; i < WordCount(ToHighlight); ++i)
     {
         foreach(string B in Blue)
         if(GetWord(ToHighlight, i) == B)
         {
             Highlighted += GetWord(ToHighlight, i) // Set Colour Somehow;
         }
         else
         {
             Highlighted += GetWord(ToHighlight, i);
         }
     }
}
public int WordCount(string ToCount)
{
     int Count = 0;
     for(int i = 0; i < ToCount.Length; ++i)
     {
         if(ToCount[i].ToString() == " ")
         {
            Count++;
         }
     }
     return Count;
}
public String GetWord(string From, int WordNum)
{
}

Upvotes: 3

Views: 331

Answers (2)

Attila
Attila

Reputation: 3406

Check these:

Syntax Highligher tutorial

Upvotes: 1

Mickey
Mickey

Reputation: 933

Take a look at this. I hope this is what you're searching for:

http://millz12.wordpress.com/2009/11/26/c-richtextbox-syntax-highlighting/

Upvotes: 2

Related Questions