gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:54:50 25 4
gpt4 key购买 nike

当用户在富文本框中写一些词时,如果该词与某个特定词匹配,那么该词的颜色应该会自动改变。

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

最佳答案

鉴于要求:

1) A User inserts some text in a RichTextBox Control.
2) If the word entered is part of a pre-defined list of words, that word should change color (so, define a relation between a word and a color).
3) When a mouse Click event is generated on a colored word, an event is raised, to notify which word was clicked.

可能的结果(复制视觉示例中的内容):

RicheTextBox write words in Colors

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

public class WordsEventArgs : EventArgs
{
private string m_word;
public WordsEventArgs(string word) { m_word = word; }
public string Word { get { return m_word; } set { m_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>();

用相关颜色的单词填充列表,然后将其绑定(bind)到 ListBox,调用 FillColoredWords() 方法(换句话说,处理将文本片段与颜色相关联的对象集合值(value)观):

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

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

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

关于c# - 如何在用户书写时在 RichTextBox 中用不同的颜色为不同的单词着色,并在单击该彩色文本时引发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48352623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com