Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
{
}
like image 465
Joe Avatar asked Jun 09 '26 02:06

Joe


1 Answers

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/

like image 198
Mickey Avatar answered Jun 11 '26 15:06

Mickey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!