富文本框查找

RichTextbox Find

我在 RichTextbox 中找到了搜索和突出显示文本的好方法 LINK

这个解决方案工作正常,但我发现了一个非常讨厌的错误,当搜索任何文本中的最后一个字符时,例如 "Hello World" 并且如果您尝试输入字母 "d"搜索字段,程序会在此处无限循环故障

while ((index = this.Find(findWhat, startSearch, findoptions)) > -1)
{
    isfind = true;
    this.SelectionBackColor = highlightColor;
    startSearch = index + 1;
}

如何修复这个错误?或者帮我找到另一个解决方案如何在 RichTextBox.

中查找和突出显示文本

是的,它有问题。您必须添加一个额外的检查以确保它不会在文本末尾之外开始搜索。像这样:

    int max = this.TextLength;
    while (startSearch < max && 
           (index = this.Find(findWhat, startSearch, findoptions)) > -1) {
        isFind = true;
        this.SelectionBackColor = highlightColor; 
        startSearch = index + 1;
    }