如何在用户书写时为RichTextBox中的不同单词涂上不同的颜色并单击该彩色文本时引发事件 [英] How to color different words with different colors in a RichTextBox while a user is writing and raise an event when that colored text is clicked

查看:75
本文介绍了如何在用户书写时为RichTextBox中的不同单词涂上不同的颜色并单击该彩色文本时引发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When a user writes some words in a rich text box, if that word matches some specific word, than the color of the that word should automatically change.

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

When the user clicks on that particular colored text, it should raise an event.

推荐答案


1)用户在RichTextBox中插入一些文本< br>
2)如果输入的任何单词是单词预定义列表的一部分,则该单词应更改颜色(定义与每个单词相关的颜色。

3)当在彩色单词上生成鼠标单击事件时,会引发一个事件。

1) An User inserts some text in a RichTextBox
2) If any word entered is part of a pre-defined list of words, that word should change color (define a color related to each word.
3) When a mouse click event is generated on a colored word, an event is raised.

可能的结果:

Possible result:

< img src = https://i.stack.imgur.com/eouW9.gif alt =在此处输入图片描述>

定义一个具有自定义EventArgs的自定义EventHandler:

Define a custom EventHandler with custom EventArgs:

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

用一些与颜色相关的单词填充列表,然后将其绑定到列表框:

Fill the list with some words an related color, then bind it to a listbox:

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()事件,评估最后输入的单词是否是要着色的单词列表的一部分:

In the richTextBox1.KeyPress() event, evaluate if the last entered word is part of the list of words to color:

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()事件:

In the richTextBox1_MouseClick() event, verify whether the click is generated on a colored word.
It true, raise the custom OnWordClicked() event:

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

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

In the custom event, add the clicked word to a textbox text:

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

这篇关于如何在用户书写时为RichTextBox中的不同单词涂上不同的颜色并单击该彩色文本时引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆