RichTextBox 删除已删除的颜色 'keywords'

RichTextBox remove color from deleted 'keywords'

我最近发现这段有趣的代码可以更改 RichTextBox 控件中 'key words' 的颜色,来自 link Color specific words in RichtextBox

问题是当单词的某些字母被删除时,单词仍然是彩色的。

例如。关键字AND是红色的,但是如果我删除了字母N,剩下的字母AD还是红色的。

我希望能够将其设置回 RichTextBox ForeColor

我知道我应该在关键字检查之前将 ForeColor 设置回白色(我的默认颜色),但我尝试过的都没有用。

有什么想法吗?

代码由 Sajeetharan

private void Rchtxt_TextChanged(object sender, EventArgs e)
    {
        this.CheckKeyword("and", Color.Red, 0);
        this.CheckKeyword("or", Color.Red, 0);
    }

private void CheckKeyword(string word, Color color, int startIndex)
{
    if (this.Rchtxt.Text.Contains(word))
    {
        int index = -1;
        int selectStart = this.Rchtxt.SelectionStart;

        while ((index = this.Rchtxt.Text.IndexOf(word, (index + 1))) != -1)
        {
            this.Rchtxt.Select((index + startIndex), word.Length);
            this.Rchtxt.SelectionColor = color;
            this.Rchtxt.Select(selectStart, 0);
            this.Rchtxt.SelectionColor = Color.Black;
        }
    }
}

您可以将完整文本的颜色重置为默认颜色(在本例中为黑色),然后运行您常用的关键字着色再次应用这些颜色。

    private void Rchtxt_TextChanged(object sender, EventArgs e)
    {
        this.CheckKeyword(Rchtxt.Text, Color.Black, 0);
        this.CheckKeyword("and", Color.Red, 0);
        this.CheckKeyword("or", Color.Red, 0);
    }

使用此答案作为结果的参考:

此 class 对象用于跟踪要着色的词以及在写入或更改这些词之一时要使用的相关颜色:
(链接的答案解释了如何用单词和相关颜色填充列表)

public class ColoredWord
{
    public string Word { get; set; }
    public Color WordColor { get; set; }
}

public List<ColoredWord> ColoredWords = new List<ColoredWord>();

RichTextBoxKeyUp()事件中,检查列表中的单词是否被插入或修改:
这里我使用了 KeyUp() 事件,因为在它被引发的那一刻,这个词已经是 inserted/modified.

private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
{
    if ((e.KeyCode >= Keys.Left & e.KeyCode <= Keys.Down)) return;
    if (e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.Alt || e.KeyCode == Keys.ControlKey) return;

    int CurrentPosition = richTextBox1.SelectionStart;
    int[] WordStartEnd;

    string word = GetWordFromPosition(richTextBox1, CurrentPosition - 1, out WordStartEnd);
    ColoredWord result = ColoredWords.FirstOrDefault(s => s.Word == word.ToUpper());
    SetSelectionColor(richTextBox1, result, CurrentPosition, WordStartEnd);

    if (e.KeyCode == Keys.Space && result == null) 
    {
        word = GetWordFromPosition(richTextBox1, CurrentPosition + 1, out WordStartEnd);
        result = ColoredWords.FirstOrDefault(s => s.Word == word.ToUpper());
        SetSelectionColor(richTextBox1, result, CurrentPosition, WordStartEnd);
    }
}

这些辅助方法用于在单词包含在列表中时为其着色并从当前插入符位置提取单词。

当一个已经着色的单词被space字符拆分时,因此两端失去它们的颜色,在KeyUp()事件中处理(代码部分为:if (e.KeyCode == Keys.Space && result == null)。

private void SetSelectionColor(RichTextBox ctl, ColoredWord word, int Position, int[] WordStartEnd)
{
    ctl.Select(WordStartEnd[0], WordStartEnd[1]);
    if (word != null)
    {
        if (ctl.SelectionColor != word.WordColor)
            ctl.SelectionColor = word.WordColor;
    }
    else
    {
        if (ctl.SelectionColor != ctl.ForeColor)
            ctl.SelectionColor = ctl.ForeColor;
    }
    ctl.SelectionStart = Position;
    ctl.SelectionLength = 0;
    ctl.SelectionColor = richTextBox1.ForeColor;
}

private string GetWordFromPosition(RichTextBox ctl, int Position, out int[] WordStartEnd)
{
    int[] StartEnd = new int[2];
    StartEnd[0] = ctl.Text.LastIndexOf((char)Keys.Space, Position - 1) + 1;
    StartEnd[1] = ctl.Text.IndexOf((char)Keys.Space, Position);
    if (StartEnd[1] == -1) StartEnd[1] = ctl.Text.Length;
    StartEnd[1] -= StartEnd[0];
    WordStartEnd = StartEnd;
    return ctl.Text.Substring(StartEnd[0], StartEnd[1]);
}