如何在用户编写时在RichTextBox中为不同颜色的不同单词着色,并在单击该彩色文本时引发事件

时间:2018-01-20 04:35:28

标签: c# winforms richtextbox

当用户在富文本框中写入某些单词时,如果该单词与某个特定单词匹配,则该单词的颜色应自动更改。

当用户点击该特定彩色文本时,它应该引发一个事件。

2 个答案:

答案 0 :(得分:2)

  

1)用户在RichTextBox中插入一些文本    2)如果输入的任何单词是预定义单词列表的一部分,则该单词应更改颜色(定义与每个单词相关的颜色。
    3)在彩色字上生成鼠标点击事件时,会引发事件。

可能的结果:

enter image description here

使用自定义EventArgs定义自定义EventHandler:

public class WordsEventArgs : EventArgs
{
    private string word;
    public WordsEventArgs(string s) { word = s; }
    public string Word { get { return word; } set { word = value; } }
}

public delegate void WordsEventHandler(object sender, WordsEventArgs e);
public event WordsEventHandler WordClicked;

protected void OnWordClicked(WordsEventArgs e) => WordClicked?.Invoke(this, e);

订阅活动:

this.WordClicked += new WordsEventHandler(this.Word_Click);

单词列表的简单类:

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

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

在列表中填入一些相关颜色的单词,然后将其绑定到列表框:

FillColoredWords();

public void FillColoredWords()
{
    ColoredWords.Add(new ColoredWord { Word = "SIMPLE", WordColor = Color.Goldenrod });
    ColoredWords.Add(new ColoredWord { Word = "COLORED", WordColor = Color.Salmon });
    ColoredWords.Add(new ColoredWord { Word = "TEXT", WordColor = Color.DarkCyan });
    this.listBox1.DisplayMember = "Word";
    this.listBox1.DataSource = ColoredWords;
}

richTextBox1.KeyPress()事件中,评估最后输入的字词是否为要着色的字词列表的一部分:

private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    int CurrentPosition = richTextBox1.SelectionStart;
    if (e.KeyChar == (char)Keys.Space && CurrentPosition > 0 && richTextBox1.Text.Length > 1)
    {
        int LastSpacePos = richTextBox1.Text.LastIndexOf((char)Keys.Space, CurrentPosition - 1);
        LastSpacePos = LastSpacePos > -1 ? LastSpacePos + 1 : 0;

        string LastWord = richTextBox1.Text.Substring(LastSpacePos, CurrentPosition - (LastSpacePos));
        ColoredWord result = ColoredWords.FirstOrDefault(s => s.Word == LastWord.ToUpper());

        richTextBox1.Select(LastSpacePos, CurrentPosition - LastSpacePos);
        if (result != null)
        {
            if (richTextBox1.SelectionColor != result.WordColor)
                richTextBox1.SelectionColor = result.WordColor;
        }
        else
        {
            if (richTextBox1.SelectionColor != richTextBox1.ForeColor)
                richTextBox1.SelectionColor = richTextBox1.ForeColor;
        }
        richTextBox1.SelectionStart = CurrentPosition;
        richTextBox1.SelectionLength = 0;
        richTextBox1.SelectionColor = richTextBox1.ForeColor;
    }
}

richTextBox1_MouseClick()事件中,验证是否在彩色字词上生成了点击。
如果是,请举起自定义OnWordClicked()事件:

private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
{
    if (richTextBox1.SelectionColor.ToArgb() != richTextBox1.ForeColor.ToArgb())
    {
        try
        {
            int WordInit = richTextBox1.Text.LastIndexOf((char)32, richTextBox1.SelectionStart);
            WordInit = WordInit > -1 ? WordInit : 0;
            int WordEnd = richTextBox1.Text.IndexOf((char)32, richTextBox1.SelectionStart);
            string WordClicked = richTextBox1.Text.Substring(WordInit, WordEnd - WordInit) + Environment.NewLine;
            OnWordClicked(new WordsEventArgs(WordClicked));
        }
        catch (Exception)
        {
            //Handle a fast doublelick: RTB is a bit dumb.
            //Handle a word auto-selection that changes the `.SelectionStart` value
        }
    }
}

在自定义事件中,将单击的单词添加到文本框文本中:

private void Word_Click(object sender, WordsEventArgs e)
{
    textBox1.AppendText(e.Word);
}

答案 1 :(得分:0)

首先将更改的事件添加到您的富文本框文本中  你需要改变文本的颜色,如果这是一个特定的词,例如:Munis,Ali

   private void Rchtxt_TextChanged(object sender, EventArgs e)
            {
                this.CheckKeyword("Munis", Color.Purple, 0);
                this.CheckKeyword("Ali", Color.Green, 0);
            }