You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#.NET中依据x/y值为RichTextBox行设置不同颜色高亮

解决C# RichTextBox中按x/y结果高亮整行的问题

我来帮你搞定这个RichTextBox的整行高亮需求!根据你描述的规则——x=0时整行红底、x=y时绿底、x≠y时橙底,我们可以通过正则提取数值+逐行设置背景色的方式实现,下面是具体方案:

核心思路

  1. 遍历RichTextBox的每一行内容
  2. 用正则表达式匹配x/y Items格式的文本,提取x和y的整数数值
  3. 根据x和y的关系,为整行设置对应的背景高亮色
  4. 处理完后恢复用户原有的选中状态和滚动位置,避免体验变差

完整实现代码

using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace YourNamespace
{
    public partial class YourForm : Form
    {
        public YourForm()
        {
            InitializeComponent();
        }

        // 调用这个方法来执行高亮逻辑
        private void HighlightResultLines()
        {
            // 保存用户原来的选中状态和滚动位置
            int originalSelectionStart = richTextBox1.SelectionStart;
            int originalSelectionLength = richTextBox1.SelectionLength;
            int originalScrollPos = richTextBox1.VerticalScroll.Value;

            // 匹配"x/y Items"格式的正则,可根据实际文本调整"Items"部分
            Regex resultRegex = new Regex(@"(\d+)/(\d+) Items");

            for (int lineIndex = 0; lineIndex < richTextBox1.Lines.Length; lineIndex++)
            {
                string currentLine = richTextBox1.Lines[lineIndex];
                Match match = resultRegex.Match(currentLine);

                if (match.Success)
                {
                    // 安全提取x和y的数值,避免格式错误导致崩溃
                    if (int.TryParse(match.Groups[1].Value, out int x) && int.TryParse(match.Groups[2].Value, out int y))
                    {
                        // 获取当前行的起始字符索引
                        int lineStartIndex = richTextBox1.GetFirstCharIndexFromLine(lineIndex);
                        // 计算整行长度(包含换行符,最后一行除外)
                        int lineTotalLength = currentLine.Length;
                        if (lineIndex < richTextBox1.Lines.Length - 1)
                        {
                            lineTotalLength += Environment.NewLine.Length;
                        }

                        // 选中整行并设置背景色
                        richTextBox1.SelectionStart = lineStartIndex;
                        richTextBox1.SelectionLength = lineTotalLength;

                        // 按规则设置颜色:x=0优先级最高
                        if (x == 0)
                        {
                            richTextBox1.SelectionBackColor = Color.LightCoral; // 柔和的红色,避免太刺眼
                        }
                        else if (x == y)
                        {
                            richTextBox1.SelectionBackColor = Color.LightGreen;
                        }
                        else
                        {
                            richTextBox1.SelectionBackColor = Color.LightSalmon; // 柔和的橙色
                        }
                    }
                }
                else
                {
                    // 对不匹配格式的行,恢复默认背景色(可选)
                    int lineStartIndex = richTextBox1.GetFirstCharIndexFromLine(lineIndex);
                    int lineTotalLength = currentLine.Length;
                    if (lineIndex < richTextBox1.Lines.Length - 1)
                    {
                        lineTotalLength += Environment.NewLine.Length;
                    }
                    richTextBox1.SelectionStart = lineStartIndex;
                    richTextBox1.SelectionLength = lineTotalLength;
                    richTextBox1.SelectionBackColor = richTextBox1.BackColor;
                }
            }

            // 恢复用户原来的选中状态和滚动位置
            richTextBox1.SelectionStart = originalSelectionStart;
            richTextBox1.SelectionLength = originalSelectionLength;
            richTextBox1.SelectionBackColor = richTextBox1.BackColor;
            richTextBox1.VerticalScroll.Value = originalScrollPos;
            richTextBox1.Refresh();
        }

        // 示例:当你更新完统计结果后调用高亮方法
        private void UpdateResults()
        {
            // 假设这里是你的统计逻辑,生成结果文本
            richTextBox1.Text = "10/10 Items\n4/10 Items\n0/10 Items\n5/7 Items";
            // 执行高亮
            HighlightResultLines();
        }
    }
}

关键细节说明

  • 正则表达式适配:如果你的行文本不是Items(比如是中文"项目"),直接修改正则里的Items为对应的文字即可。
  • 颜色选择:我用了柔和的浅色系(LightCoral、LightGreen等),比纯红纯绿更适合阅读,你可以根据需求换成Color.Red这类纯色。
  • 异常防护:用int.TryParse代替直接强制转换,避免行格式错误导致程序崩溃。
  • 用户体验优化:保存并恢复选中状态和滚动位置,不会打乱用户的操作状态。

内容的提问来源于stack exchange,提问作者user6157196

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:20:57