Hi everyone,
I’m currently using RadRichTextEditor to print logs. I have the following scenario:
- The first line prints "Hello" in black font.
- The second line prints "world" in red font.
How can I change the font color for different lines in RadRichTextEditor?
Thank you!
2 Answers, 1 is accepted
public void AppendLog(RadRichTextEditor radRichTextEditor, string messages)
{
// Create a RadDocumentEditor instance for editing the document
RadDocumentEditor editor = new RadDocumentEditor(radRichTextEditor.Document);
// Create a new Span with the message and set the ForeColor
Span messageSpan = new Span(messages);
if (messages.Contains("world"))
{
messageSpan.ForeColor = System.Drawing.Color.Red;
}
else
{
messageSpan.ForeColor = System.Drawing.Color.Black;
}
// Insert the span and a new line
editor.InsertInline(messageSpan);
editor.InsertInline(new Break(BreakType.LineBreak));
}
Hello everyone, I have already solved the above issue. The above C# code shows how to modify the font color when printing with RadRichTextEditor. Thank you all for your attention. If you encounter the above issue in the future, you can directly use the provided code to make the modification.
Best regards,
Kevin
Hello, Kevin,
I have gone through all the posts and comments in this forum thread and finally it seems that you managed to achieve the desired result.
The Span class in RadRichTextEditor represents an inline object that allows you to display formatted text. The Spans can only be used in the context of a Paragraph class. As the spans are inline elements they get placed one after another and the text inside them gets wrapped to the next line if the space is insufficient. More information is available here: Span - UI for WinForms Documentation - Telerik UI for WinForms
Using Span objects and apply ForeColor is the right way to color text lines with different colors. Your contribution of sharing a code snippet to the community is appreciated, as it allows other clients with similar queries to benefit from it. I updated your Telerik Points for your effort.
If you have any other questions, do not hesitate to contact us.
Thanks for your replying Nadya.
Now I am using the Telerik RadRichTextEditor control to print logs with the following function, but I have noticed that when printing a large volume of logs, the memory usage on the computer where the software is running keeps increasing. I closed all other functionalities and only used the Telerik RadRichTextEditor control for log printing. I added a length limit and clear the content when it exceeds the limit, but I found that memory usage does not decrease after clearing the content. Is this an issue with the control? The memory growth rate far exceeds that of the RichTextEditor control. The RichTextEditor control itself does not show significant growth in memory usage. Thank you.
public static void SetText(RadRichTextEditor m_RichTextBox, string strText, Color m_Color)
{
// Limit length 20000
int maxLength = 20000;
// Obtain length
int currentLength = m_RichTextBox.Document.EnumerateChildrenOfType<Telerik.WinForms.Documents.Model.Span>().Sum(span => span.Text.Length);
// Clear content
if (currentLength > maxLength)
{
m_RichTextBox.Document = new Telerik.WinForms.Documents.Model.RadDocument();
}
// Create a RadDocumentEditor instance for editing the document
RadDocumentEditor editor = new RadDocumentEditor(m_RichTextBox.Document);
// Create a new Span with the message and set the ForeColor
Telerik.WinForms.Documents.Model.Span messageSpan = new Telerik.WinForms.Documents.Model.Span(strText);
messageSpan.ForeColor = m_Color;
messageSpan.FontSize = 13;
// Insert the span and a new line
editor.InsertInline(messageSpan);
//editor.InsertInline(new Break(BreakType.LineBreak));
}
Hello, Kevin,
Following the provided information, I used the shared code snippet in my test project, but I was not able to observe increased memory usage. Could you please specify the number of logs you are generating? Also, how big are the span messages that you use?
Considering the provided code snippet, this operation is not expected to increase the memory usage. RadRichTextEditor is a powerful control and is indented to deal with even more complex documents. You already said that have closed all other functionality and only use RadRichTextEditor control for log printing, but it is strange that this causes such problem with memory on your end. Are you sure that there isn't any other process or application that is consuming a great amount of memory in parallel with running the project? Have you tried running it on another machine?
I am providing my test project for your reference. Can you please refer to it and let me know if you can reproduce there the problem. The memory consumption stays at the expected level on my end. Feel free to replicate the problem there, then get the project back to me. Once we replicate the issue locally, we would be able to make an adequate analysis of the precise case and provide further assistance.
If possible you can provide to us your sample project, and thus we can investigate it further. In such case, I would recommend you to submit a support ticket in order to respect your privacy and any sensitive data that you may have.
I am looking forward to your reply.RadRichTextEditor
separately from the original functionality of my software, continuously printing log content in a loop, I find no significant memory growth. I have realized that the issue is likely related to my development. Additionally, I tested the program you provided and observed no significant memory growth either. I am continuing to troubleshoot, but I can confirm that the issue is not with the RadRichTextEditor
control itself. Apologies for the confusion.Hello, Kevin,
Thank you for sharing your findings with us. In case you have any other questions regarding RadRichTextEditor or any other RadContol, do not hesitate to contact me.
this.radRichTextEditor1.RichTextBoxElement.Document.Selection.SelectAll();
this.radRichTextEditor1.RichTextBoxElement.ChangeTextForeColor(System.Drawing.Color.Red);
this.radRichTextEditor1.RichTextBoxElement.Document.Selection.Clear();
I found out how to change the color of the printed content in the Telerik RadRichTextEditor, but the above code selects all the content to change the color. Currently, I only need to change the color of the current line. How can I modify the above code? Thank you.