SoFunction
Updated on 2025-03-07

C# How to display text in different colors in RichTextBox

This article example describes how C# displays different colors of text in RichTextBox. Share it for your reference. The specific implementation method is as follows:

#region logging, support other thread accesspublic delegate void LogAppendDelegate(Color color, string text); 
/// <summary> 
/// Append text/// </summary> 
/// <param name="color">Text color</param>/// <param name="text">Show text</param>public void LogAppend(Color color, string text) 
{ 
  ("\n"); 
   = color; 
  (text); 
} 
/// &lt;summary&gt; 
/// Display error log/// &lt;/summary&gt; 
/// &lt;param name="text"&gt;&lt;/param&gt; 
public void LogError(string text) 
{ 
  LogAppendDelegate la = new LogAppendDelegate(LogAppend); 
  (la, , ("HH:mm:ss ") + text); 
} 
/// &lt;summary&gt; 
/// Show warning message/// &lt;/summary&gt; 
/// &lt;param name="text"&gt;&lt;/param&gt; 
public void LogWarning(string text) 
{ 
  LogAppendDelegate la = new LogAppendDelegate(LogAppend); 
  (la, , ("HH:mm:ss ") + text); 
} 
/// &lt;summary&gt; 
/// Show information/// &lt;/summary&gt; 
/// &lt;param name="text"&gt;&lt;/param&gt; 
public void LogMessage(string text) 
{ 
  LogAppendDelegate la = new LogAppendDelegate(LogAppend); 
  (la, , ("HH:mm:ss ") + text); 
} 
#endregion

I hope this article will be helpful to everyone's C# programming.