突出显示richtextbox中所有搜索到的单词

在我的RichtextBox ,如果我写的如下。

这是我的笔,
他的笔是美丽的。

现在我搜索单词“is”然后输出如下。

所有“是”应该突出显示。

此致,Khilen

关于什么:

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

使用此方法,您可以同时突出显示多个单词。 或者您可以轻松跳过foreach部分并使用它来突出显示一个单词。

 private void HighlightWords(string[] words) { foreach (string word in words) { int startIndex = 0; while (startIndex < rich.TextLength) { int wordStartIndex = rich.Find(word, startIndex, RichTextBoxFinds.None); if (wordStartIndex != -1) { rich.SelectionStart = wordStartIndex; rich.SelectionLength = word.Length; rich.SelectionBackColor = Color.Yellow; } else break; startIndex += wordStartIndex + word.Length; } } } 

看起来这样做会。

http://www.dotnetcurry.com/ShowArticle.aspx?ID=146

 int start = 0; int indexOfSearchText = 0; private void btnFind_Click(object sender, EventArgs e) { int startindex = 0; if(txtSearch.Text.Length > 0) startindex = FindMyText(txtSearch.Text.Trim(), start, rtb.Text.Length); // If string was found in the RichTextBox, highlight it if (startindex >= 0) { // Set the highlight color as red rtb.SelectionColor = Color.Red; // Find the end index. End Index = number of characters in textbox int endindex = txtSearch.Text.Length; // Highlight the search string rtb.Select(startindex, endindex); // mark the start position after the position of // last search string start = startindex + endindex; } } public int FindMyText(string txtToSearch, int searchStart, int searchEnd) { // Unselect the previously searched string if (searchStart > 0 && searchEnd > 0 && indexOfSearchText >= 0) { rtb.Undo(); } // Set the return value to -1 by default. int retVal = -1; // A valid starting index should be specified. // if indexOfSearchText = -1, the end of search if (searchStart >= 0 && indexOfSearchText >=0) { // A valid ending index if (searchEnd > searchStart || searchEnd == -1) { // Find the position of search string in RichTextBox indexOfSearchText = rtb.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None); // Determine whether the text was found in richTextBox1. if (indexOfSearchText != -1) { // Return the index to the specified search text. retVal = indexOfSearchText; } } } return retVal; } // Reset the richtextbox when user changes the search string private void textBox1_TextChanged(object sender, EventArgs e) { start = 0; indexOfSearchText = 0; } 

这里有一篇文章可以享受http://www.dotnetcurry.com/ShowArticle.aspx?ID=146

这将同时显示所有搜索的标准。

使用:1个文本框(输入要搜索的文本)和1个按钮(运行搜索)。

在文本框中输入搜索条件,然后按搜索按钮。

  // On Search Button Click: RichTextBox ("rtb") will display all the words inside the document private void btn_Search_Click(object sender, EventArgs e) { try { if (rtb.Text != string.Empty) {// if the ritchtextbox is not empty; highlight the search criteria int index = 0; String temp = rtb.Text; rtb.Text = ""; rtb.Text = temp; while (index < rtb.Text.LastIndexOf(txt_Search.Text)) { rtb.Find(txt_Search.Text, index, rtb.TextLength, RichTextBoxFinds.None); rtb.SelectionBackColor = Color.Yellow; index = rtb.Text.IndexOf(txt_Search.Text, index) + 1; rtb.Select(); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } } } 

}

我同意上面的Alex Jolig的解决方案。 但我找到了一件事,就是最后一行,

startIndex + = wordStartIndex + word.Length;

不应该有+ =,相反,

startIndex = wordStartIndex + word.Length;

这会奏效。

如果你只想匹配整个单词你可以使用它,请注意这会忽略大小写,而| s \ b意味着复数会突出显示,例如Cat匹配猫而不是caterpiller:

  public static void HighlightText(RichTextBox myRtb, string word, Color color) { if (word == string.Empty) return; var reg = new Regex(@"\b" + word + @"(\b|s\b)",RegexOptions.IgnoreCase); foreach (Match match in reg.Matches(myRtb.Text)) { myRtb.Select(match.Index, match.Length); myRtb.SelectionColor = color; } myRtb.SelectionLength = 0; myRtb.SelectionColor = Color.Black; } 
  private void button3_Click(object sender, EventArgs e) { if (textBox1.Text != "") { for (int i = 0; i < richTextBox1.TextLength; i++) { richTextBox1.Find(textBox1.Text, i, RichTextBoxFinds.None); richTextBox1.SelectionBackColor = Color.Red; } } else { for (int i = 0; i < richTextBox1.TextLength; i++) { richTextBox1.SelectAll(); richTextBox1.SelectionBackColor = Color.White; } } }[lets make it!][1]