Вот что у меня есть для функции «Найти дальше». Он находится на VB.net, так как я сейчас работаю над проектом TAFE, но вы можете легко конвертировать его в C #. Это творит чудеса для меня.
У меня есть основной richtextbox с именем «RichTextBox1», где находится текст, затем у меня есть текстовое поле с именем «ToolStripSearchTextBox», в которое я ввожу то, что я хочу найти, и кнопка «ToolStripButton2», которая вызывает метод «FindNext_Click ()» при нажатии.
Эта функция «Найти далее» не чувствительна к регистру из-за RichTextBoxFinds.None. Не стесняйтесь менять это по своему желанию.
// Find next
Dim searchIndex As Integer = 0
Dim lastSearch As String
Private Sub FindNext_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
// If the search textbox is empty, focus on it
If (ToolStripSearchTextBox.Text = String.Empty) Then
ToolStripSearchTextBox.Focus()
Return
End If
// If user changed their search term, reset the index
If (ToolStripSearchTextBox.Text <> lastSearch) Then
searchIndex = 0
End If
lastSearch = ToolStripSearchTextBox.Text
// If the character(s) exist, update the index. Otherwise, set the index to -1
Try
searchIndex = RichTextBox1.Find(ToolStripSearchTextBox.Text, searchIndex, RichTextBoxFinds.None)
Catch ex As ArgumentOutOfRangeException
searchIndex = -1
End Try
// Character(s) exists, focus on the main textbox and then select the character(s)
If (searchIndex <> -1) Then
RichTextBox1.Focus()
RichTextBox1.SelectionStart = searchIndex
RichTextBox1.SelectionLength = ToolStripSearchTextBox.Text.Length
searchIndex = searchIndex + 1
Else // No occurances of text or user has highlghted last remaining word. Let the user know they have reached the end of the document and reset the index
searchIndex = 0
//RichTextBox1.SelectionStart = 0
//RichTextBox1.SelectionLength = 0
MessageBox.Show("End of document", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub