Perform "Syntax Highlighting" in TRichEdit - Programmers Heaven

Howdy, Stranger!

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


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Perform "Syntax Highlighting" in TRichEdit

SkyterrorSkyterror Posts: 16Member
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?


  • VampireFEVampireFE Posts: 36Member
    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 :)
  • PiSymbolPiSymbol Posts: 13Member
    You can use RichEdit but there is already a component. [link=]Try SynEdit.[/link]
Sign In or Register to comment.