在RichtextBox中标记特定颜色的单词

12

当用户在富文本框中输入特定单词如“while”或“if”时,我该如何使这些单词变为紫色而没有任何问题?我尝试了不同的代码,但它们都无法使用。这些代码类似于以下代码:

if (richTextBox.Text.Contains("while"))
  {
    richTextBox.Select(richTextBox.Text.IndexOf("while"), "while".Length);
    richTextBox.SelectionColor = Color.Aqua;
  }

此外,我希望当用户删除单词或单词中的一个字母时,单词会恢复到默认颜色。我正在使用带有代码编辑器的Visual C#编写程序。

谢谢。


@MicrosoftDN:WinForm。 - user3256753
2
看起来你想要一些语法高亮的东西,那么可以查看这篇文章:一个具有语法高亮的文本框/富文本框? - scheien
请查看此帖子中的答案 在RichTextBox字符串中着色不同部分 - scheien
4个回答

26

在你的富文本框文本更改事件中添加一个事件

  private void Rchtxt_TextChanged(object sender, EventArgs e)
        {
            this.CheckKeyword("while", Color.Purple, 0);
            this.CheckKeyword("if", Color.Green, 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;
            }
        }
    }

你的代码非常完美。谢谢!但是,当我删除“while”中的一个单词(例如:“wile”)时,它的颜色没有恢复到默认值?! - user3256753
1
要修复颜色,只需在检查之前设置前景色。 - Demetry

6

这是你可以做的事情,我建议使用正则表达式查找所有匹配项,以防它出现多次。还要记住,字符串查找可能是一个字符串列表,位于for循环之外,以考虑多个单词,但这应该让你开始了。

//dont foget to use this at the top of the page
using System.Text.RegularExpressions;

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        string find = "while";
        if (richTextBox1.Text.Contains(find))
        {
            var matchString = Regex.Escape(find);
            foreach (Match match in Regex.Matches(richTextBox1.Text, matchString))
            {
            richTextBox1.Select(match.Index, find.Length);
            richTextBox1.SelectionColor = Color.Aqua;
            richTextBox1.Select(richTextBox1.TextLength, 0);
            richTextBox1.SelectionColor = richTextBox1.ForeColor;
            };
        }
    }

2

您可以使用 richTextBox1_KeyDown 事件

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                String abc = this.richTextBox1.Text.Split(' ').Last();
                if (abc == "while")
                {
                    int stIndex = 0;
                    stIndex = richTextBox1.Find(abc, stIndex, RichTextBoxFinds.MatchCase);
                    richTextBox1.Select(stIndex, abc.Length);
                    richTextBox1.SelectionColor = Color.Aqua;
                    richTextBox1.Select(richTextBox1.TextLength, 0);
                    richTextBox1.SelectionColor = richTextBox1.ForeColor;
                }
            }
        }

1
如果您想突出显示正则表达式而不是单词,可以使用以下方法:
   private void ColorizePattern(string pattern, Color color)
   {
            int selectStart = this.textBoxSrc.SelectionStart;                
            foreach (Match match in Regex.Matches(textBoxSrc.Text, pattern))
            {
                textBoxSrc.Select(match.Index, match.Length);
                textBoxSrc.SelectionColor = color;
                textBoxSrc.Select(selectStart, 0);
                textBoxSrc.SelectionColor = textBoxSrc.ForeColor;
            };      
   }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接