2016-06-27 27 views
4

私は検索機能を持っています。テキストの先頭から最後までリッチテキストボックス内の文字列を検索できます。私は最後の一致を見つけたときに始まります。ここでVisual C#で文字列を逆方向に参照することはできますか?

コード:

 #region Search 

     private void txtSearch_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     start = 0; 
     end = 0; 
    }  

    //Searchfield 
    private void toolStripTextBoxSearch_Click(object sender, EventArgs e) 
    { 

    }//end TextBoxSearch 

    public int FindMyText(string txtToSearch, int searchStart, int searchEnd) 
    { 
     // Set the return value to -1 by default. 
     int retVal = -1; 

     // A valid starting index should be specified. 
     if (searchStart >= 0) 
     { 
      // A valid ending index 
      if (searchEnd > searchStart || searchEnd == -1) 
      { 
       // Find the position of search string in RichTextBox 
       indexOfSearchText = richTextBox.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; 
    }//end FindMyText 

    private void buttonSearch_left_Click(object sender, EventArgs e) 
    { 

    } 

    private void buttonSearch_right_Click(object sender, EventArgs e) 
    { 
     int startindex = 0; 

     if (txtSearch.Text.Length > 0) 
     { 
      startindex = FindMyText(txtSearch.Text, start, richTextBox.Text.Length); 
     } 
      // If string was not found report it 
      if (startindex < 0) 
      { 
       if (stringfoundflag==1) 
       { 
       startindex = FindMyText(txtSearch.Text, 0, richTextBox.Text.Length); //Start at Pos. 0 
       } 
       else 
       { 
        MessageBox.Show("Not found in Textfield", "Search", MessageBoxButtons.OK, MessageBoxIcon.Information); 
       } 
      }//end if 

      if (startindex >= 0) 
      { 
       stringfoundflag = 1; 
      // Set the highlight color as red 
      //richTextBox.SelectionColor = Color.Red; 
      // Find the end index. End Index = number of characters in textbox 
      int endindex = txtSearch.Text.Length; 
       // Highlight the search string 
       richTextBox.Select(startindex, endindex); 
       // mark the start position after the position of 
       // last search string 
       start = startindex + endindex; 
     } 
    } 

    // Reset the richtextbox when user changes the search string 
    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     start = 0; 
     indexOfSearchText = 0; 
    } 

    private void txtSearch_Enter(object sender, EventArgs e) 
    { 
     if (txtSearch.Text == "Search") 
     { 
      txtSearch.Text = ""; 
     } 
    } 

    private void txtSearch_Leave(object sender, EventArgs e) 
    { 
     if (txtSearch.Text == "") 
     { 
      txtSearch.Text = "Search"; 
     } 
    } 

    #endregion 

私は2つのボタン1件の検索右と左の1、開始時の説明のように右の作品を持っています。

質問:

は私が私の解決策と左側にあるボタンの右からsearchfunctionを逆にすることができ、または私は全体の検索機能を変更する必要がありますか?

例:最初の順番で開始し、最後の一致よりも先にジャンプし、次に最後から次のようにジャンプします。

+0

をsniffi /yab8wkhy(v=vs.110).aspx)。備考欄を参照してください。 – rene

+0

あなたの質問のタグはOKですか?あなたのコードのどれもasp.netまたはwebformsではないようです – rene

+0

私はエディタを追加したタグを削除しました。あなたの助けrexのためのthx私は解決策を見つけた – sniffi

答えて

0

こんにちは私は私の問題の解決策を見つけました。誰かがこれを助けることを願っています。

コード:優しい希望に

 public int FindMyTextleft(string txtToSearch, int searchStart, int searchEnd) 
    { 
     // Set the return value to -1 by default. 
     int retVal = -1; 

     // A valid starting index should be specified. 
     if (searchStart >= 0) 
     { 
      // A valid ending index 
      if (searchEnd > searchStart || searchEnd == -1) 
      { 
       // Find the position of search string in RichTextBox 
       indexOfSearchText = richTextBox.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.Reverse); 
       // Determine whether the text was found in richTextBox1. 
       if (indexOfSearchText != -1) 
       { 
        // Return the index to the specified search text. 
        retVal = indexOfSearchText; 
       } 
      } 
     }//end FindMyTextleft 
     return retVal; 
    }//end FindMyText 

    private void buttonSearch_left_Click(object sender, EventArgs e) 
    { 
     int startindex = 0; 

     if (txtSearch.Text.Length > 0 & stringfoundflag == 1) 
     { 
      startindex = FindMyTextleft(txtSearch.Text, startindex, end); 
     } 

     else if (txtSearch.Text.Length > 0) 
     { 
      startindex = FindMyTextleft(txtSearch.Text, start, richTextBox.Text.Length); 
     } 
     // If string was not found report it 
     if (startindex < 0) 
     { 
      if (stringfoundflag == 1) 
      { 
       startindex = FindMyTextleft(txtSearch.Text, 0, richTextBox.Text.Length); //Start at Pos. 0 
      } 
      else 
      { 
       MessageBox.Show("Not found in Textfield", "Search", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 
     }//end if 

     if (startindex >= 0) 
     { 
      stringfoundflag = 1; 
      // Set the highlight color as red 
      //richTextBox.SelectionColor = Color.Red; 
      // Find the end index. End Index = number of characters in textbox 
      int endindex = txtSearch.Text.Length; 
      // Highlight the search string 
      richTextBox.Select(startindex, endindex); 
      // mark the start position after the position of 
      // last search string 
      end = startindex; 
     } 
    }//buttonSearch_left_Click 

はあなたが唯一[RichTextBoxFinds.Reverse](https://msdn.microsoft.com/en-us/libraryに最後のパラメータを変更する必要が

関連する問題