【问题标题】:Color specific words in RichtextBox为 RichtextBox 中的特定单词着色
【发布时间】:2014-03-25 16:33:53
【问题描述】:

我怎样才能使当用户在富文本框中键入“while”或“if”等特定词时,这些词会变成​​紫色而没有任何问题?我尝试了不同的代码,但没有一个可用。代码如下:

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

另外,我希望当用户删除单词或从单词中删除一个字母时,该单词的颜色会恢复为默认颜色。我正在制作一个带有代码编辑器的程序。

我正在使用 Visual c#。

谢谢。

【问题讨论】:

标签: c# winforms


【解决方案1】:

将事件添加到您的富文本框中已更改,

  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”)时,它的颜色不会变回默认值???!!!
  • 要修复颜色,只需在检查前设置前景色
【解决方案2】:

这是你可以做的事情,我建议使用正则表达式来查找单词的所有匹配项,以防它多次出现。另请记住,字符串 find 可以是 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;
            };
        }
    }

【讨论】:

    【解决方案3】:

    你可以使用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;
                    }
                }
            }
    

    【讨论】:

      【解决方案4】:

      如果您想突出显示正则表达式而不是单词,则可以使用:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-26
        • 2014-05-15
        • 1970-01-01
        • 2015-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多