C#Winform使用扩展方法自定义富文本框(RichTextBox)字体颜色

实现效果

调用方法

 rtxtLog.AppendTextColorful(richTextBox1,DateTime.Now.ToString(), Color.Red);

完整代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            rtxtLog.AppendTextColorful(richTextBox1,"abc123", Color.Green);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            rtxtLog.AppendTextColorful(richTextBox1,DateTime.Now.ToString(), Color.Red);
        }
        
    }

    /// <summary>
    /// https://www.cnblogs.com/bobositlife/p/csharp-winform-change-richtextbox-font-color-using-static-extension-method.html#:~:text=C%23Winform%E4%BD%BF%E7%94%A8%E6%89%A9%E5%B1%95%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E5%AF%8C%E6%96%87%E6%9C%AC%E6%A1%86%20%28RichTextBox%29%E5%AD%97%E4%BD%93%E9%A2%9C%E8%89%B2%20%E5%9C%A8%E5%8E%9F%E7%94%9FWinform%E7%9A%84RichTextBox%E4%B8%AD%EF%BC%8C%E6%98%AF%E6%B2%A1%E6%9C%89%E8%BF%99%E7%A7%8D%E8%AE%BE%E7%BD%AE%E9%80%89%E9%A1%B9%E7%9A%84%E3%80%82%20%E5%A6%82%E9%9C%80%E5%AE%9E%E7%8E%B0%E4%BB%A5%E4%B8%8A%E6%8F%8F%E8%BF%B0%E7%9A%84%E5%8A%9F%E8%83%BD%EF%BC%8C%E6%88%91%E4%BB%AC%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8.NET%E7%9A%84%E9%9D%99%E6%80%81%E6%89%A9%E5%B1%95%E6%96%B9%E6%B3%95%E6%9D%A5%E5%A4%84%E7%90%86%E3%80%82%20%E5%AE%9E%E7%8E%B0%E6%89%A9%E5%B1%95,%E6%96%B9%E6%B3%95%E7%9A%84%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95%E6%9C%AC%E8%BA%AB%E9%83%BD%E5%BF%85%E9%A1%BB%E6%98%AF%E9%9D%99%E6%80%81%E7%9A%84%EF%BC%8C%E5%A6%82%E6%9E%9C%E4%BD%A0%E5%AF%B9%E6%89%A9%E5%B1%95%E6%96%B9%E6%B3%95%E8%BF%98%E4%B8%8D%E6%98%AF%E5%A4%AA%E4%BA%86%E8%A7%A3%EF%BC%8C%E5%BB%BA%E8%AE%AE%E5%85%88%E6%9F%A5%E9%98%85%E7%9B%B8%E5%85%B3%E6%96%87%E6%A1%A3%E8%B5%84%E6%96%99%E3%80%82%20%E6%88%91%E8%BF%99%E9%87%8C%E5%B0%B1%E6%8A%8A%E5%AE%9E%E7%8E%B0%E6%94%B9%E5%8F%98RichTextBox%E5%AD%97%E4%BD%93%E9%A2%9C%E8%89%B2%E7%9A%84%E6%89%A9%E5%B1%95%E6%96%B9%E6%B3%95%E8%B4%B4%E5%87%BA%20%E5%9C%A8%E5%88%A9%E7%94%A8C%23%E5%BC%80%E5%8F%91Winform%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%E7%9A%84%E6%97%B6%E5%80%99%EF%BC%8C%E6%88%91%E4%BB%AC%E6%9C%89%E5%8F%AF%E8%83%BD%E4%BD%BF%E7%94%A8RichTextBox%E6%9D%A5%E5%AE%9E%E7%8E%B0%E5%AE%9E%E6%97%B6%E6%98%BE%E7%A4%BA%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%E6%97%A5%E5%BF%97%E7%9A%84%E5%8A%9F%E8%83%BD%EF%BC%8C%E6%97%A5%E5%BF%97%E5%8F%88%E5%88%86%E4%B8%BA%EF%BC%9A%E4%B8%80%E8%88%AC%E6%B6%88%E6%81%AF%EF%BC%8C%E8%AD%A6%E5%91%8A%E6%8F%90%E7%A4%BA%20%E5%92%8C%E9%94%99%E8%AF%AF%E7%AD%89%E7%B1%BB%E5%88%AB%E3%80%82%20%E4%B8%BA%E4%BA%86%E6%9B%B4%E5%A5%BD%E5%9C%B0%E5%8C%BA%E5%88%86%E4%B8%8D%E5%90%8C%E7%B1%BB%E5%9E%8B%E7%9A%84%E6%97%A5%E5%BF%97%EF%BC%8C%E6%88%91%E4%BB%AC%E9%9C%80%E8%A6%81%E4%BD%BF%E7%94%A8%E4%B8%8D%E5%90%8C%E7%9A%84%E9%A2%9C%E8%89%B2%E6%9D%A5%E8%BE%93%E5%87%BA%E5%AF%B9%E5%BA%94%E7%9A%84%E6%97%A5%E5%BF%97%EF%BC%8C%E6%AF%94%E5%A6%82%EF%BC%9A%E4%B8%80%E8%88%AC%E6%B6%88%E6%81%AF%E4%B8%BA%E7%BB%BF%E8%89%B2%EF%BC%8C%E8%AD%A6%E5%91%8A%E6%8F%90%E7%A4%BA%E7%9A%84%E7%94%A8%E6%A9%99%E8%89%B2%EF%BC%8C%E9%94%99%E8%AF%AF%E7%9A%84%E7%94%A8%E7%BA%A2%E8%89%B2%E5%AD%97%E4%BD%93%E3%80%82
    /// </summary>
    public static class rtxtLog
    {
        public static void AppendTextColorful(this RichTextBox rtBox, string text, Color color, bool addNewLine = true)
        {
            if (addNewLine)
            {
                text += Environment.NewLine;
            }
            rtBox.SelectionStart = rtBox.TextLength;
            rtBox.SelectionLength = 0;
            rtBox.SelectionColor = color;
            rtBox.AppendText(text);
            rtBox.SelectionColor = rtBox.ForeColor;
        }
    }
}

 参考链接在完整代码内

特此记录

anlog

2024年4月13日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值