RichTextBox从已删除的“关键字”中删除颜色

时间:2018-05-07 21:04:12

标签: c# winforms colors richtextbox

我最近发现这个有趣的代码可以改变RichTextBox控件中'关键字'的颜色,从此链接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;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

使用此答案作为结果的参考:
How to color different words with different colors in a RichTextBox

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

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

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

RichTextBox KeyUp()事件中,检查列表中的单词是否正在插入或修改:
我在这里使用KeyUp()事件,因为在它被提升的那一刻,该词已被插入/修改。

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

这些是帮助方法,用于在Word列入列表时为其着色,并从当前插入位置提取单词。

当一个已着色的单词被拆分一个空格字符的情况,因此两端丢失它们的颜色,在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]);
}