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

馋奶兔 提交于 2019-11-27 09:50:32

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:

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

Subscribe the event:

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

Simple Class for the list of words:

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

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

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

first of all add an event to your rich box text changed,then you need to change the color of text if that is an specific word like for example : Munis , Ali

   private void Rchtxt_TextChanged(object sender, EventArgs e)
            {
                this.CheckKeyword("Munis", Color.Purple, 0);
                this.CheckKeyword("Ali", Color.Green, 0);
            }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!