Khi*_*yar 12 c# highlight richtextbox find
在我RichtextBox,如果我写的如下.
这是我的笔,
他的笔是美丽的.
现在我搜索单词"is"然后输出如下.
应突出显示所有"是".
Oma*_*mar 19
关于什么:
static class Utility {
public static void HighlightText(this RichTextBox myRtb, string word, Color color) {
if (word == string.Empty)
return;
int s_start = myRtb.SelectionStart, startIndex = 0, index;
while((index = myRtb.Text.IndexOf(word, startIndex)) != -1) {
myRtb.Select(index, word.Length);
myRtb.SelectionColor = color;
startIndex = index + word.Length;
}
myRtb.SelectionStart = s_start;
myRtb.SelectionLength = 0;
myRtb.SelectionColor = Color.Black;
}
}
Run Code Online (Sandbox Code Playgroud)