RichTextBox Find

TwoWing

Well-known member
Joined
Jan 6, 2006
Messages
71
Location
Near Portsmouth, England
Programming Experience
Beginner
Hello. I have this code for finding a string in a RichTextBox -
VB.NET:
 Public Sub SelectMyString()
        rtbWord.Find(TextBox1.Text, RichTextBoxFinds.MatchCase)
        rtbWord.SelectionFont = New Font("Verdana", 12, FontStyle.Bold)
        rtbWord.SelectionColor = Color.Red
    End Sub
- how can I change the code so that every occurence of the text is changed as well? Thank you.
 
Just wrap it in a while loop.

VB.NET:
While(rtbWord.Find(TextBox1.Text, RichTextBoxFinds.MatchCase) <> -1)
        rtbWord.SelectionFont = New Font("Verdana", 12, FontStyle.Bold)
        rtbWord.SelectionColor = Color.Red
end while
 
Rich Text box Find

Thanks for your reply. I tried it but kept crashing! I did some work and came up with this. It seems OK.
VB.NET:
' This method demonstrates retrieving line numbers that 
            ' indicate the location of a particular word
            ' contained in a RichTextBox. The line numbers are zero-based.
	   ' Reset the results box.
            TextBox1.Text = ""
	  ' Get the word to search from from TextBox2.
            Dim searchWord As String = TextBox2.Text
	  Dim index As Integer = 0
	  'Declare an ArrayList to store line numbers.
            Dim lineList As New System.Collections.ArrayList
            Do
                ' Find occurrences of the search word, incrementing  
                ' the start index. 
                index = rtbWork.Find(searchWord, index + 1, _
                    RichTextBoxFinds.MatchCase)
          	‘This to color the word.
                rtbWork.SelectionColor = Color.Blue
	      If (index <> -1) Then
            ' Find the word's line number and add the line 
                    'number to the arrayList. 
                    lineList.Add(rtbWork.GetLineFromCharIndex(index))
                End If
            Loop While (index <> -1)
	' Iterate through the list and display the line numbers in TextBox1.
            Dim myEnumerator As System.Collections.IEnumerator = _
         lineList.GetEnumerator()
            If lineList.Count <= 0 Then
                TextBox1.Text = searchWord & " was not found"
            Else
                Console.WriteLine("Index = " & index)
                TextBox1.SelectedText = searchWord & " was found on 				line(s):"
                While (myEnumerator.MoveNext)
                    TextBox1.SelectedText = myEnumerator.Current & " "
	End While
            End If
Ta.
 
Back
Top