Mega Code Archive

 
Categories / Delphi / Graphic
 

Adding Multi Colored Text to a TRichEdit

Title: Adding Multi-Colored Text to a TRichEdit Question: This explains how to add multi-color text to a TRichEdit. I wasn't able to find any examples on the Web, so I came up with this. Answer: To add colored text to a TRichEdit box, just add the following procedure to your application: // BEGIN CODE SNIPPET // // ... procedure AddText(szText : String; clColor : TColor; RichEdit : TRichEdit); begin // Set the selected text color RichEdit.SelAttributes.Color := clColor; // Add the text RichEdit.SelText := szText; // Set the selected text back to the default RichEdit.SelAttributes.Color := clWindowText; end; // ... // END CODE SNIPPET // Then call it from your code: // BEGIN CODE SNIPPET // // ... AddText('This text is RED!', clRed, RichEdit1) // ... // END CODE SNIPPET // This code can be very useful. You can insert new lines by either using RichEdit1.Lines.Add('') or by using #10#13 (new line) in the text you're adding. I hope you find it useful!