Answered Giving Colors to special words in a RichTextBox

  • Tuesday, April 10, 2012 4:08 PM
     
     

    I have a RichTextBox.I want to change the colour of specific  words.For example I want the word "int" to come in blue colour in a RichTextBox as soon as the text is changed.

    Application Details: CLR->Windows Form Application->Visual C++

    Thanks for your consideration

    -Yashas samaga


    The first 2 words written by beginners "Hello, World". #include<brain.h> :)

All Replies

  • Tuesday, April 10, 2012 6:03 PM
     
     

    have a look for CHARFORMAT2 structure.

    Thanks


    Rupesh Shukla

  • Wednesday, April 11, 2012 5:57 AM
     
     Answered

    I have a RichTextBox.I want to change the colour of specific  words.For example I want the word "int" to come in blue colour in a RichTextBox as soon as the text is changed.

    Application Details: CLR->Windows Form Application->Visual C++

    Thanks for your consideration

    -Yashas samaga


    The first 2 words written by beginners "Hello, World". #include<brain.h> :)

    you can check with the following MSDN

    How to: Set Font Attributes for the Windows Forms RichTextBox Control


    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/

  • Wednesday, April 11, 2012 4:30 PM
     
     

    I have a RichTextBox.I want to change the colour of specific  words.For example I want the word "int" to come in blue colour in a RichTextBox as soon as the text is changed.

    Application Details: CLR->Windows Form Application->Visual C++

    Thanks for your consideration

    -Yashas samaga


    The first 2 words written by beginners "Hello, World". #include<brain.h> :)

    you can check with the following MSDN

    How to: Set Font Attributes for the Windows Forms RichTextBox Control


    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/

    Sorry for not explaining the situation properly.

    I know to set a color of text of a RTB,but actually what I wanted was Syntax Highlighting.

    I did not know this word "SyntaxHighlighting" before.


    The first 2 words written by beginners "Hello, World". #include<brain.h> :)

  • Wednesday, April 11, 2012 5:39 PM
     
     

    If you want to Syntax Highlight in that scenario simple select the text by using appropriate method. Nothing else you have to do in my last post i mentioned charformat didn't observe you are not using Winform app

    Thanks


    Rupesh Shukla

  • Thursday, April 12, 2012 1:34 PM
     
     Answered

    Just as a heads up from someone who had done syntax highlighting in a Rich Text box before -- it is the wrong tool for the job.  Yes you can kind of get it done by monitoring the EN_CHANGE notification, but it is painful at best.  Most of the time you feel like you are fighting the built-in formatting of the rich edit box.

    I went the route of writing one from scratch -- not something I would recommend either.  You might check out Scintilla which is a free source code editor you can integrate.  Its license is quite permissive and unlike the Rich Text it is designed for source editing with syntax highlighting and all the other goodies you want in a code editor.  I wish I had known about it before I wrote one from scratch.