Author Topic: Syntax highlighting RTB in C#  (Read 4331 times)

0 Members and 2 Guests are viewing this topic.

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Syntax highlighting RTB in C#
« on: August 13, 2011, 08:58:24 pm »
I'm working on an IDE and the first thing I want to tackle/complete is the text editor. Right now I'm working on the syntax highlighting part of it. Strings highlight perfectly, and so do keywords. However, the part where it un-highlights keywords that have been partially deleted seems to not want to cooperate.

Here's my code, for anyone who thinks they can help, or just wants to take a peek at it ;)
(I comment a lot, so it should be fairly easy to understand)
Spoiler For Code:
Code: [Select]
    //Initiate vars
    string[] lines = this.Lines;
    int pos = 0;
    int cpos = this.SelectionStart;
    int line = this.GetLineFromCharIndex(cpos);

    //Let's see if there are even any keywords in the line.
    //If not, we'll exit
    bool cont = false;
    foreach (string kw in rsrvdWrds)
        if (lines[line].Contains(kw))
            cont = true;

    //If the current line doesn't contain any words,
    if (!cont)
        return;     //Exit

    //Get the exact beginning char index of the line
    for (int i = 0; i < line; i++)
        //+1 because of \n
        pos += lines[i].Length + 1;

    //Now pos is the index of the first char on the current line
    //Reset line colors (THESE ARE THE FOUR LINES THAT *SHOULD* RESET THE LINE COLOR)
    this.SelectionStart = pos;                  //Go to beginning of line
    this.SelectionLength = lines[line].Length;  //Select the line
    this.SelectionColor = Color.Black;          //Turn it black
    this.SelectionLength = 0;                   //Deselect the line

    //For each match of keyword in the current line
    foreach (Match kw in reservedWords.Matches(lines[line])) {

        this.SelectionStart = pos + kw.Index;
        this.SelectionLength = kw.Length;
        this.SelectionColor = Color.Blue;

    }

    //Highlight any strings that might be in the line's string
    this.HighlightString(line);

    //Resume current position
    this.SelectionLength = 0;
    this.SelectionStart = cpos;
    this.SelectionColor = Color.Black;
Oh, by the way, this is an extended control. By that, I mean it's a control that inherits from the RichTextBox. That's why it says "this" instead of "rtbCode" or something like that.
« Last Edit: August 13, 2011, 09:00:19 pm by BlakPilar »

SirCmpwn

  • Guest
Re: Syntax highlighting RTB in C#
« Reply #1 on: August 13, 2011, 09:00:56 pm »
May I propose an alternative?
ICSharpCode has a great open source syntax highlighting text box for WinForms and for WPF.  You should download the source code to SharpDevelop and try it out.  That's what tiDE uses.

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Syntax highlighting RTB in C#
« Reply #2 on: August 13, 2011, 09:04:54 pm »
Yeah, I know about their TextEditor and AvalonEdit, but I'd prefer not to use any 3rd-party controls for the time being. This is something I've been meaning to get around doing for a long time, and I now have the perfect motivation.

I'm also not sure if I want the end product to be open-source or not, and from my understanding it would need to be open-source if I used TE or AE :\
« Last Edit: August 13, 2011, 09:05:04 pm by BlakPilar »

SirCmpwn

  • Guest
Re: Syntax highlighting RTB in C#
« Reply #3 on: August 13, 2011, 09:18:35 pm »
That's fine if you want to roll your own, but for the record, you can use TextEdit and AvalonEdit and keep your programs closed-source.

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Syntax highlighting RTB in C#
« Reply #4 on: August 13, 2011, 09:31:17 pm »
Oh okay, cool. I'll look into using one of their editors, but I'm still going to try to finish this one.
« Last Edit: August 13, 2011, 09:31:24 pm by BlakPilar »