C# richTextBox显示不同颜色文字时间:2021-02-16 16:02:14 C# richTextBox显示不同颜色文字 #region 日志记录、支持其他线程访问 public delegate void LogAppendDelegate(Color color, string text); /// <summary> /// 追加显示文本 /// </summary> /// <param name="color">文本颜色</param> /// <param name="text">显示文本</param> public void LogAppend(Color color, string text) { richTextBoxRemote.AppendText("/n"); richTextBoxRemote.SelectionColor = color; richTextBoxRemote.AppendText(text); } /// <summary> /// 显示错误日志 /// </summary> /// <param name="text"></param> public void LogError(string text) { LogAppendDelegate la = new LogAppendDelegate(LogAppend); richTextBoxRemote.Invoke(la, Color.Red, DateTime.Now.ToString("HH:mm:ss ") + text); } /// <summary> /// 显示警告信息 /// </summary> /// <param name="text"></param> public void LogWarning(string text) { LogAppendDelegate la = new LogAppendDelegate(LogAppend); richTextBoxRemote.Invoke(la, Color.Violet, DateTime.Now.ToString("HH:mm:ss ") + text); } /// <summary> /// 显示信息 /// </summary> /// <param name="text"></param> public void LogMessage(string text) { LogAppendDelegate la = new LogAppendDelegate(LogAppend); richTextBoxRemote.Invoke(la, Color.Black, DateTime.Now.ToString("HH:mm:ss ") + text); } #endregion