本文介绍了如何在用户书写时为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. 推荐答案 可能的结果: Possible result: 定义一个具有自定义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中的不同单词涂上不同的颜色并单击该彩色文本时引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 13:14