Perform "Syntax Highlighting" in TRichEdit

Hi, guys
Currently I have a very confusing problems. Lately I am developing a small project. In my project, I use a RichEdit component. I wonder how can we perform action like in IDE programs which it is syntax highlighting. If user enter some word that program recognized, it will changes the words color to red or blue (depend on what syntax) while the others that are not the syntax are remain black.

Is there anybody could help me solve this problem?


  • This could be a way to do it:

    [code]void __fastcall TForm1::RichEdit1Change(TObject *Sender)
    ::SendMessage(RichEdit1->Handle, WM_SETREDRAW, false, 0);

    int sp = RichEdit1->SelStart;

    for(int i=0;iText.Length();i++)
    if(RichEdit1->Text[i+1] >= 'A' && RichEdit1->Text[i+1] <= 'Z') // capital letters
    RichEdit1->SelStart = i;
    RichEdit1->SelLength = 1;
    RichEdit1->SelAttributes->Color = clBlue;
    RichEdit1->SelAttributes->Style = RichEdit1->SelAttributes->Style << fsBold;
    RichEdit1->SelStart = i;
    RichEdit1->SelLength = 1;
    RichEdit1->SelAttributes->Color = clBlack;
    RichEdit1->SelAttributes->Style = RichEdit1->SelAttributes->Style >> fsBold;

    RichEdit1->SelStart = sp;

    ::SendMessage(RichEdit1->Handle, WM_SETREDRAW, true, 0);

    I know that code is ... well, lame, but, meh, I don't care :D

    What it does is:
    1. disables drawing
    2. it goes through all elements in RichEdit, changing capital letters to bold and blue
    3. enables drawing

    Should be pretty easy to search for particular strings in there and change them.

    I hope it helps :)
  • You can use RichEdit but there is already a component. [link=]Try SynEdit.[/link]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!