Hello,

I'm trying to make a sub that will highlight the text of a RTB that is passed. Here is the code:

    Private Sub SearchAll(ByVal selectedRichTextBox As RichTextBox)
        Dim startPos As Int16 = 0
        Dim matchPos As Integer = selectedRichTextBox.Find(TextBoxSearchFor.Text, startPos, selectedRichTextBox.Text.Length, RichTextBoxFinds.None)
        selectedRichTextBox.SelectionBackColor = Color.Yellow
        While matchPos >= 0
            selectedRichTextBox.Select(matchPos, TextBoxSearchFor.Text.Length)
            startPos = matchPos + TextBoxSearchFor.Text.Length
            matchPos = selectedRichTextBox.Find(TextBoxSearchFor.Text, startPos, selectedRichTextBox.Text.Length, RichTextBoxFinds.None)
        End While

        MsgBox("You have finished searching!")
        startPos = 0
    End Sub

I've read acticles on this but I can't figure out what I'm doing wrong.
Awaiting for someone who could help me and thanks in advance,
Kappash

1st of all, why do you pass a RichTextBox referene as argument (into this method)?
If you do this code in the class (form) where contorl is, you can access to it directly.
Otherwise, your method must return this same reference to form where control really is!
So method must have a return type of RichTextBox.

Next, here is example code that does what you asked for:

Dim len As Integer = Me.richTextBox1.TextLength
Dim index As Integer = 0
Dim lastIndex As Integer = Me.richTextBox1.Text.LastIndexOf(Me.textBox1.Text)

While index < lastIndex
    Me.richTextBox1.Find(Me.textBox1.Text, index, len, RichTextBoxFinds.None)
    Me.richTextBox1.SelectionBackColor = Color.Yellow
    index = Me.richTextBox1.Text.IndexOf(Me.textBox1.Text, index) + 1
End While

and you can check here here how it must be done to select (highlight) text.
Hope it helps,
bye

Hello Mitja,

I pass it as an argument because the function will be used by different RTB's. And as for the snippet and the link, it should do the exact same as what I've made or am I mistaken?

Solved it. Here are the 4 functions I have written.

Region "Functies"
Private Sub SearchNext(ByVal selectedRichTextBox As RichTextBox)
    Dim matchPos As Integer = startPos
    matchPos = selectedRichTextBox.Text.ToLower().IndexOf(TextBoxSearchFor.Text.ToLower(), matchPos)
    If matchPos >= 0 Then
        selectedRichTextBox.SelectionStart = matchPos
        selectedRichTextBox.SelectionLength = TextBoxSearchFor.Text.Length
        selectedRichTextBox.SelectionBackColor = Color.Yellow
        selectedRichTextBox.Focus()
        startPos = TextBoxSearchFor.Text.Length + matchPos
    Else
        MessageBox.Show("Het doorzoeken van de tekst is voltooid.")
        startPos = 0
    End If
End Sub

Private Sub SearchAll(ByVal selectedRichTextBox As RichTextBox)
    startPos = 0
    Dim matchPos As Integer = startPos
    matchPos = selectedRichTextBox.Text.ToLower().IndexOf(TextBoxSearchFor.Text.ToLower(), startPos)

    While matchPos >= 0
        selectedRichTextBox.SelectionStart = matchPos
        selectedRichTextBox.SelectionLength = TextBoxSearchFor.Text.Length
        selectedRichTextBox.SelectionBackColor = Color.Yellow
        selectedRichTextBox.Focus()

        startPos = matchPos + TextBoxSearchFor.Text.Length

        matchPos = selectedRichTextBox.Text.ToLower().IndexOf(TextBoxSearchFor.Text.ToLower(), startPos)
    End While

    MsgBox("Het doorzoeken van de tekst is voltooid.")
    startPos = 0
End Sub

Private Sub ReplaceCurrent(ByVal selectedRichTextBox As RichTextBox)
    Dim matchPos As Integer = startPos
    matchPos = selectedRichTextBox.Text.ToLower().IndexOf(TextBoxSearchFor.Text.ToLower(), matchPos)
    If matchPos >= 0 Then
        selectedRichTextBox.SelectionStart = matchPos
        selectedRichTextBox.SelectionLength = TextBoxSearchFor.Text.Length
        selectedRichTextBox.Focus()

        startPos = TextBoxSearchFor.Text.Length + matchPos

        selectedRichTextBox.SelectedText = TextBoxReplaceWith.Text
        selectedRichTextBox.SelectionStart = matchPos
        selectedRichTextBox.SelectionLength = TextBoxReplaceWith.Text.Length
    Else
        MessageBox.Show("Het doorzoeken van de tekst is voltooid.")
        startPos = 0
    End If

End Sub

Private Sub ReplaceAll(ByVal selectedRichTextBox As RichTextBox)
    startPos = 0
    Dim matchPos As Integer = startPos
    matchPos = selectedRichTextBox.Text.ToLower().IndexOf(TextBoxSearchFor.Text.ToLower(), startPos)

    While matchPos >= 0
        selectedRichTextBox.SelectionStart = matchPos
        selectedRichTextBox.SelectionLength = TextBoxSearchFor.Text.Length
        selectedRichTextBox.Focus()

        startPos = matchPos + TextBoxSearchFor.Text.Length

        selectedRichTextBox.SelectedText = TextBoxReplaceWith.Text
        selectedRichTextBox.SelectionStart = matchPos
        selectedRichTextBox.SelectionLength = TextBoxReplaceWith.Text.Length

        matchPos = selectedRichTextBox.Text.ToLower().IndexOf(TextBoxSearchFor.Text.ToLower(), startPos)
    End While

    MsgBox("Het doorzoeken van de tekst is voltooid.")
    startPos = 0
End Sub
End Region

Sorry for the misuse of the code block. ^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.