自动文本高亮显示我非常非常新的C#,只是写了一些计算器,文本编辑器和数据库客户端在学校近10年前:)不是我试图做一个工具,我和我的同事们以更简单的方式查看痕迹和日志。我们所知的Notepad ++,我们每天使用文本突出显示,样式,但是关闭Notepad ++后,这些高亮会丢失。 因此,我现在的目标是制作相同的文本编辑器,但它可以保存您的工作。目前我正在研究该功能,因此当我选择一些文本时,它将在整个文档中搜索相同内容并突出显示它,例如红色背景。我添加了这一个:C#:在富文本
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
int startIndex = 0;
while (startIndex<richTextBox1.TextLength)
{
int wordStartIndex = richTextBox1.Find(richTextBox1.SelectedText, startIndex, RichTextBoxFinds.None);
if (wordStartIndex != -1)
{
richTextBox1.SelectionStart = wordStartIndex;
richTextBox1.SelectionLength = richTextBox1.SelectionLength;
richTextBox1.SelectionBackColor = Color.Red;
}
else
break;
startIndex += wordStartIndex + richTextBox1.SelectionLength;
}
}
但它给了我“StackOverFlow”,因为我有一个循环在这里。你能帮我一下吗? 我想我需要运行2个搜索来避免循环 - 一个在选择索引之前,一个在之后。或者也许有更简单的选择?
谢谢大家,你们!