2016-07-11 11 views
1

リッチテキストボックスを検索する検索機能を設定しました。それは、テキストボックスを通過し、一致するすべての異なるケースを強調表示します。リッチテキストボックスには名前が表示され、次の行に移動するので、強調表示されたものだけでなく、名前全体を別のテキストボックスにエクスポートします。これは私がこれまでに検索機能を持っていたものです。検索と移動

 private void Search_Button_Click(object sender, EventArgs e) 
     { 
     int index = 0; 
     int count = 0; 
     string temp = Display_Rich_Text_Box.Text; 
     //bool k; 
     Display_Rich_Text_Box.Text = ""; 
     Display_Rich_Text_Box.Text = temp; 
     string[] fullName; 

     while (index <= Display_Rich_Text_Box.Text.LastIndexOf(Search_Text_Box.Text)) 
     { 
      //Searches and locates the text you are searching for 
      Display_Rich_Text_Box.Find(Search_Text_Box.Text, index, Display_Rich_Text_Box.TextLength, RichTextBoxFinds.None); 

      //Color Selection: Hightlights in yellow 
      Display_Rich_Text_Box.SelectionBackColor = Color.Yellow; 
      count = count + 1; 

      fullName = Display_Rich_Text_Box.split("\n") 

      //Will search through rest of document or until it cannot continue 
      index = Display_Rich_Text_Box.Text.IndexOf(Search_Text_Box.Text, index) + 1; 
     } 

     // if (count > 0) 
     //{ 
     Form2 f = new Form2(count.ToString(), fullName.tostring()); 
     f.ShowDialog(); 
     } 

(テキストボックスは別のフォームにあり、このフォームには見つかった一致数も表示されます)。

テキストをハイライトした後に改行したときに、テキストボックスを分割しようとしました。

例:(Search_Text_Boxで入力)倍

フォルダ フォルダB 新しいデザイン 写真

を検索するだから、それはSearch_Text_Boxで入力いただきました、それは強調して検索するとき。私がしようとしているのは、Display_Text_Boxを担当する文字列を分割することで、ユーザーがfoldを入力すると別のテキストボックスにフォルダAとフォルダBが表示されます。テキストボックスは異なる形式です。事前に助けてくれてありがとう。

+0

あなたは「全体の名前」とはどういう意味ですか?リッチテキストボックスで名前を検索していますか? –

+0

プログラムはフォルダ名とファイル名を検索し、リッチテキストボックスに表示します。それぞれの名前は独自の行を取得します。例:folder1(改行)、folder2(改行)...検索ボックスに「fol」と入力して検索を実行すると、フォールドを強調表示します。しかし、私はfolとその行の残りの部分をexportして、folder2を新しいテキストボックスにも移動する必要があります。それは役に立ちますか? – Tasha

+0

入力を '\ n'で分割すると役立つでしょうか? –

答えて

1

私は行を分割するためにあなたの機能を変更しました:

private void Search_Button_Click(object sender, EventArgs e) 
    { 
     var lines = Display_Rich_Text_Box.Text.Split('\n'); 
     var count = 0; 

     // To get the text from the whole line (Which is the whole name you're looking for) 
     foreach (string line in lines) 
     { 
      // If the line doesn't have the text you're looking for 
      if (!line.Contains(Search_Text_Box.Text)) continue; 

      count++; 

      // Add the index of the whole input plus the index of the text within the line 
      var index = lines.IndexOf(line) + line.IndexOf(Search_Text_Box.Text); 

      //Searches and locates the text you are searching for 
      Display_Rich_Text_Box.Find(Search_Text_Box.Text, index, 
       Display_Rich_Text_Box.TextLength, RichTextBoxFinds.None); 

      //Color Selection: Hightlights in yellow 
      Display_Rich_Text_Box.SelectionBackColor = Color.Yellow; 

      //DO SOMETHING WITH LINE 
      var wholeName = line; 
     } 

     Form2 f = new Form2(searchCount); 
     f.ShowDialog(); 
    } 
+0

Regexはどのライブラリですか?既知のキーワードとして表示されない – Tasha

+0

System.Text.RegularExpressionsを使用しています。 –

+0

本当にうまくいってくれてありがとうBlue Eyed Behemoth – Tasha

関連する問題