Home >Backend Development >C++ >How Can I Add Color to Specific Sections of Text in a RichTextBox?
Add color to specific text segment in RichTextBox
Displaying multi-color text in RichTextBox is a common programming task. This can be challenging, especially when working with text from a variety of sources. To achieve this, we can leverage the RichTextBoxExtensions class, which extends the AppendText method to include a color parameter.
For example, consider the following text:
<code>[9:23pm] 用户:我的消息在这里。</code>
To color this text we will use an extension method like this:
<code class="language-csharp">var box = new RichTextBox { Dock = DockStyle.Fill, Font = new Font("Courier New", 10) }; box.AppendText("[" + DateTime.Now.ToShortTimeString() + "]", Color.Red); box.AppendText(" "); box.AppendText("用户0001", Color.Green); box.AppendText(": "); box.AppendText("访问被拒绝", Color.Blue); box.AppendText(Environment.NewLine);</code>
This method allows precise control of the color of specific text portions in the RichTextBox. Note that any potential flicker issues can be mitigated by utilizing the techniques outlined in the referenced C# Corner article.
The above is the detailed content of How Can I Add Color to Specific Sections of Text in a RichTextBox?. For more information, please follow other related articles on the PHP Chinese website!