1,931 questions
You can try using this code:
This is for the Replace All function:
If Not txtSearchWord.Text = "" Then
If RichTextBox1.Text.Contains(txtSearchWord.Text) Then
RichTextBox1.Text = Replace(RichTextBox1.Text, txtSearchWord.Text, txtReplaceWord.Text)
Exit Sub
Else
MessageBox.Show("The specified text [" & txtSearchWord.Text & "] was not found.", "Failed Search", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtSearchWord.Text = ""
txtSearchWord.Focus()
Exit Sub
End If
End If
MessageBox.Show("No search text specified.", "Invalid Search", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
And this is for the Find All function:
Static Start As Integer = -1
Dim Index As Integer
Index = RichTextBox1.Text.IndexOf(txtSearchWord.Text, Start + 1)
If Not Index = -1 Then
RichTextBox1.Focus()
RichTextBox1.SelectionStart = Index
RichTextBox1.SelectionLength = txtSearchWord.Text.Length
RichTextBox1.ScrollToCaret()
Start = Index
Else
MessageBox.Show("Found all the instances of [" & txtSearchWord.Text & "]", "End Search")
Start = -1
End If
This code assumes that the RichTextBox is named: RichTextBox1, and there are two textboxes named: txtSearchWord and txtReplaceWord.