'how can i search text within the text box in visual basic

The search function i have done is working however it only finds the word once where i have difficulty is that i cant get the search function to read the whole textbox and find all the words that entered in the search for text box

Private Sub btnSearch_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnSearch.Click
  Dim Search As String
  Dim Where As Long

  ' Get search string from user.
  Search = InputBox("Enter text to be found:")

  ' Find string in text.
  Where = InStr(TheText.Text, Search)

  If Where <> 0 Then
    TheText.Focus()
    TheText.SelectionStart = Where - 1
    TheText.SelectionLength = Len(Search)
  Else
    MsgBox("String not found.")
  End If

  txtR.Text = CountWords(Search)

  Sorting.Items.Add(txtR.Text)
End Sub


Solution 1:[1]

Text box control doesn't allow multiple selects like that, but the Rich Textbox control does. It would essentially be the same code that you have, but you would need a position number to keep track of the most recent find and loop until there are no more instances of the search string.

Solution 2:[2]

If you just want to show the matching items, you can use a Regular Expression. The items that match your criteria would be included in the RegEx.Matches collection. See http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.matches.aspx#Y527

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 UnhandledExcepSean
Solution 2 Jim Wooley