如何在 RichTextBox 控件中 select 特定颜色的文本?

How to select specific colored text in a RichTextBox control?

我想 select RichTextBox 中的特定彩色文本。例如,如果我想 select 下图中显示的蓝色文本,我该怎么做?

这是一个函数,它将 return 所有字符位置具有特定颜色:

List<int> getColorPositions(RichTextBox rtb, Color col)
{
    List<int> pos = new List<int>();
    for (int i = 0; i < rtb.Text.Length; i++)
    {
        rtb.SelectionStart = i;
        richTextBox1.SelectionLength = 1;
        if (rtb.SelectionColor.ToArgb() == col.ToArgb() ) pos.Add(i);
    }
    return pos;
}

您可以选择 select 您感兴趣的部分..

如果你确定只有一个部分有你想要的颜色你可以select那部分像这样:

rtb.SelectionStart = pos[0]; 
rtb.SelectionLength = pos.Count;

但是当然可能有几个部分,您需要决定要select/highlight。请注意,一次只能 一个 部分文本 selected/highlighted!

基本技巧如下:

  • 从特定索引(可能是当前光标位置)开始,逐个循环遍历 RichTextBox 和 select 每个字符的文本。
  • 您可以查看 SelectionColor 属性 以确定 selected 字符是否具有您要查找的颜色。
  • 找到所需颜色的字符后,记住它的索引。
  • 继续循环,直到找到不是您感兴趣的颜色的字符(或者您已到达文本末尾);记住那个索引。
  • 您现在可以select两个索引之间的文本:Select(firstIndex, secondIndex - firstIndex)

您可以为 RichTextBox 创建一个扩展方法,将上面的内容封装到一个很好用的易于使用的方法中:

public static class RichTextExtensions
{
    /// <summary>
    /// Searches for text in a RichTextBox control by color and selects it if found.
    /// </summary>
    /// <param name="rtb">The target RichTextBox.</param>
    /// <param name="color">The color of text to search for.</param>
    /// <param name="startIndex">The starting index to begin searching from (optional).  
    /// If this parameter is null, the search will begin at the point immediately
    /// following the current selection or cursor position.</param>
    /// <returns>If text of the specified color was found, the method returns the index 
    /// of the character following the selection; otherwise, -1 is returned.</returns>
    public static int SelectTextByColor(this RichTextBox rtb, Color color, int? startIndex = null)
    {
        if (rtb == null || rtb.Text.Length == 0) return -1;
        if (startIndex == null)
        {
            if (rtb.SelectionLength > 0) 
                startIndex = rtb.SelectionStart + rtb.SelectionLength;
            else if (rtb.SelectionStart == rtb.Text.Length) 
                startIndex = 0;
            else 
                startIndex = rtb.SelectionStart;
        }
        int matchStartIndex = rtb.FindTextByColor(color, startIndex.Value, true);
        if (matchStartIndex == rtb.Text.Length)
        {
            rtb.Select(matchStartIndex, 0);
            return -1;
        }
        int matchEndIndex = rtb.FindTextByColor(color, matchStartIndex, false);
        rtb.Select(matchStartIndex, matchEndIndex - matchStartIndex);
        return matchEndIndex;
    }

    private static int FindTextByColor(this RichTextBox rtb, Color color, int startIndex, bool match)
    {
        if (startIndex < 0) startIndex = 0;
        for (int i = startIndex; i < rtb.Text.Length; i++)
        {
            rtb.Select(i, 1);
            if ((match && rtb.SelectionColor == color) || 
                (!match && rtb.SelectionColor != color))
                return i;
        }
        return rtb.Text.Length;
    }
}

您可以调用如下所示的扩展方法。请注意,如果您的 RichTextBox 控件启用了 HideSelection(默认情况下会启用),您需要将焦点设置回 RichTextBox 才能看到 select编辑文本。

richTextBox1.SelectTextByColor(Color.Blue);
richTextBox1.Focus();

如果您希望搜索从特定索引(例如文本的开头)而不是当前光标位置开始,您可以将该索引作为第二个参数传递给该方法:

richTextBox1.SelectTextByColor(Color.Blue, 0);