2012-08-07 10 views
11

私のRichtextBoxに、私が以下のように書いていれば。リッチテキストボックス内のすべての検索語をハイライト表示

これは私のペンです。
彼のペンは美しいです。

ここで私は単語 "を検索する"と 出力は以下のようになります。

すべての「is」を強調表示する必要があります。

よろしく、これはそれを行うだろうと同じように、 Khilen

+2

も「これは」と「彼」強調されるべきで存在「である」:猫は猫ではなくCATERPILLERに一致しますか? – digEmAll

答えて

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の位置から1単語を2回探していますか?while(myRtb.Text.IndexOf(word、startIndex)!= -1){ int index = myRtb.Text.IndexOf(word、startIndex); '? whileループで見つかったインデックスを保存するべきだと思います。 –

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

これは最初のオカレンスだけを見つけます。一致するすべての単語をハイライトしたい –

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部分を簡単にスキップして、それを使用して1つの単語だけを強調表示することもできます。

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の解決法に同意します。しかし、私は1つ、最後の行を見つけた

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

コードを提供するだけでなく、あなたの答えを説明してください。また、あなたのソリューションが動作しているようだが、この質問はかなり古く、すでに受け入れられている答えがある。あなたはより新しい質問でもっと高く評価されるでしょう:) –

+0

OK。ありがとうございます –

+0

このサイトがどのように役立つかについての情報は、ヘルプセンターで見つけることができます。例:回答方法:http://stackoverflow.com/help/how-to-answer –

関連する問題