本文实例讲述了C#在RichTextBox中显示不同颜色文字的方法。分享给大家供大家参考。具体实现方法如下:
#region 日志记录、支持其他线程访问 public delegate void LogAppendDelegate(Color color, string text); ////// 追加显示文本 /// /// 文本颜色 /// 显示文本 public void LogAppend(Color color, string text) { richTextBoxRemote.AppendText("n"); richTextBoxRemote.SelectionColor = color; richTextBoxRemote.AppendText(text); } ////// 显示错误日志 /// /// public void LogError(string text) { LogAppendDelegate la = new LogAppendDelegate(LogAppend); richTextBoxRemote.Invoke(la, Color.Red, DateTime.Now.ToString("HH:mm:ss ") + text); } ////// 显示警告信息 /// /// public void LogWarning(string text) { LogAppendDelegate la = new LogAppendDelegate(LogAppend); richTextBoxRemote.Invoke(la, Color.Violet, DateTime.Now.ToString("HH:mm:ss ") + text); } ////// 显示信息 /// /// public void LogMessage(string text) { LogAppendDelegate la = new LogAppendDelegate(LogAppend); richTextBoxRemote.Invoke(la, Color.Black, DateTime.Now.ToString("HH:mm:ss ") + text); } #endregion
希望本文所述对大家的C#程序设计有所帮助。



