Function to count how many times a string occurs with Visual Basic

Function to count how many times a string occurs

 Private Function FindWords(ByVal TextSearched As String, ByVal Paragraph As String) As Integer

        Dim location As Integer = 0

        Dim occurances As Integer = 0

        Do

            location = TextSearched.IndexOf(Paragraph, location)

            If location <> -1 Then

                occurances += 1

                location += Paragraph.Length

            End If

        Loop Until location = -1

        Return occurances

    End Function

Then add the following code to "Check" button click event:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        MsgBox("The word " & TextBox2.Text & " has occured " & FindWords(TextBox1.Text, TextBox2.Text) & " times!!")

    End Sub