C# RichTextBox插入带颜色数据

 #region 日志记录、支持其他线程访问  

        public delegate void LogAppendDelegate(Color color, string text);

        public void LogAppendMethod(Color color, string text)
        {
            if (!richTextBoxOutPut.ReadOnly)
                richTextBoxOutPut.ReadOnly = true;
            this.richTextBoxOutPut.Select(this.richTextBoxOutPut.Text.Length, 0);
            this.richTextBoxOutPut.Focus();
            richTextBoxOutPut.SelectionColor = color;
            richTextBoxOutPut.AppendText(text);
        }

        public void LogError(string text)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppendMethod);
            richTextBoxOutPut.Invoke(la, Color.Red, DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
        }
        public void LogWarning(string text)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppendMethod);
            richTextBoxOutPut.Invoke(la, Color.Blue, DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
        }
        public void LogMessage(string text)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppendMethod);
            richTextBoxOutPut.Invoke(la, Color.Green, DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
        }

        #endregion

 

posted @ 2018-05-09 14:56  zhaogaojian  阅读(3480)  评论(0编辑  收藏  举报