First, if memory serves you will need to use a RichTextboxBox control for your txtClientArea control
The code below can be used like so:
To Search for all strings
Code:
Call search(TextBoxSearch.Text, txtClientArea.Text, True)
Code:
''' <summary>
''' Search's the client area and highlights the searched text
''' </summary>
''' <param name="searchValue">Value to search for</param>
''' <param name="searchControl">Richtext control</param>
''' <param name="completeWord">Matches only complete words if true else finds a reference</param>
''' <remarks></remarks>
Private Sub search(searchValue As String, searchControl As RichTextBox, completeWord As Boolean)
Dim regPattern As String = String.Format("{1}{0}{1}", searchValue, If(completeWord = True, "\b", ""))
Dim regex As New Regex(regPattern)
For Each valueFound As Match In regex.Matches(searchControl.Text)
searchControl.Select(valueFound.Index, valueFound.Length)
searchControl.SelectionColor = Color.Red
Next
End Sub
To Replace all searched strings:
Code:
Call replaceString(TextBoxSearch.Text, txReplace.Text, txtClientArea, True)
Code:
''' <summary>
''' Search's the client area and replaces and matches found
''' </summary>
''' <param name="searchValue">Value to search for</param>
''' <param name="replaceValue">Replacement Value</param>
''' <param name="searchControl">Richtext Control</param>
''' <param name="completeWord">Matches only complete words if true else finds a reference</param>
''' <remarks></remarks>
Private Sub replaceString(searchValue As String, replaceValue As String, searchControl As RichTextBox, completeWord As Boolean)
Dim regPattern As String = String.Format("{1}{0}{1}", searchValue, If(completeWord = True, "\b", ""))
If Regex.Match(searchControl.Text, regPattern).Success Then
searchControl.Text = Regex.Replace(searchControl.Text, regPattern, replaceValue)
MessageBox.Show("Replacement Complete", "Process Finished", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
Throw New Exception("No search value found")
End If
End Sub
You also need to Import System.Text.RegularExpressions at the top of your code
Code:
Imports System.Text.RegularExpressions
Hope this helps