【问题标题】:RichTextBox remove color from deleted 'keywords'RichTextBox 从已删除的“关键字”中删除颜色
【发布时间】:2018-10-17 17:51:18
【问题描述】:

我最近发现这个有趣的代码可以改变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;
        }
    }
}

【问题讨论】:

    标签: c# winforms colors richtextbox


    【解决方案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);
        }
    

    【讨论】:

    • 谢谢你,这成功了
    【解决方案2】:

    将此答案用作结果的参考:
    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>();
    

    RichTextBoxKeyUp()事件中,检查列表中的单词是否被插入或修改:
    这里我使用了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);
        }
    }
    

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

    当一个已经着色的单词被空格字符分割,因此两端失去它们的颜色,在KeyUp()事件中处理(代码部分是:if (e.KeyCode == Keys.Space &amp;&amp; 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]);
    }
    

    【讨论】:

    • 谢谢,这很有帮助!
    猜你喜欢
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 2018-10-23
    • 2022-07-22
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多