2012-08-07 148 views

回答

16

什么:

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

你为什么要为一个词将从startIndex位置搜索两次所有字:(!myRtb.Text.IndexOf(字,则startIndex)= -1)',而{ int index = myRtb.Text.IndexOf(word,startIndex);'? 我想你应该在while循环中保存找到的索引。 – 2012-08-12 11:26:49

0

看起来这会做到这一点。

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

这会发现只有第一次出现,我想强调的是匹配 – 2012-08-07 18:38:23

0

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

使用: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"); } 
    } 
} 

}

3

使用这种方法,您将能够同时选中多个单词。或者您可以轻松地跳过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; 
     } 
    } 
} 
0

我同意上面的Alex Jolig的解决方案。但我发现一件事,那最后一行,

startIndex + = wordStartIndex + word.Length;

不应有+ =,相反,

的startIndex = wordStartIndex + word.Length;

这将工作。

0

如果你只是想整个单词,你可以使用此匹配,注意,这忽略大小写,也是| S \ B表示复数得到例如强调猫匹配猫但不是蚂蚱:

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; 
    } 
0
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] 
+0

请解释你的答案,而不是仅仅提供代码。另外,虽然你的解决方案似乎有效,但这个问题已经很老了,已经有了一个可以接受的答案。你的帮助将更多的赞赏在新的问题:) – 2017-05-07 20:24:57

+0

确定。感谢 – 2017-05-08 04:14:03

+0

您可以在帮助中心找到关于本网站如何工作的大量信息,例如:如何回答:http://stackoverflow.com/help/how-to-answer – 2017-05-08 09:22:32

相关问题