C# RichTextBox从已删除的';关键词';

C# RichTextBox从已删除的';关键词';,c#,winforms,colors,richtextbox,C#,Winforms,Colors,Richtextbox,我最近发现了这个有趣的代码,它改变了RichTextBox控件中“关键字”的颜色 问题是,当该单词的某些字母被删除时,该单词仍然是彩色的。 关键字是和,它是红色的,但是如果我删除字母N,广告中剩余的字母仍然是红色的。 我希望能够将其设置回RichTextBoxForeColor private void SetSelectionColor(RichTextBox ctl, ColoredWord word, int Position, int[] WordStartEnd) { ctl.

我最近发现了这个有趣的代码,它改变了
RichTextBox
控件中“关键字”的颜色

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

关键字是和,它是红色的,但是如果我删除字母N,广告中剩余的字母仍然是红色的。

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

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]);
}
我知道我可能应该在关键字检查之前将
前景色
设置回白色(我的默认颜色),但我没有尝试任何效果。

有什么想法吗?

编码依据


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

    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);
    }

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

此类对象用于跟踪要添加颜色的单词以及写入或更改其中一个单词时要使用的相关颜色:
(链接答案解释了如何用单词和相关颜色填充列表)

这些是helpers方法,用于在单词包含在列表中时为其着色,以及从当前插入符号位置提取单词。

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


谢谢你,这很有帮助!谢谢你,这很有效
public class ColoredWord
{
    public string Word { get; set; }
    public Color WordColor { get; set; }
}

public List<ColoredWord> ColoredWords = new List<ColoredWord>();
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);
    }
}
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]);
}