Tag Archives: search

How to Search/Find Text in a Textbox control in VB.NET, VB 2008, 2010, and Higher

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 4.50 out of 5)
 

Most any kind of word processing application from the basic Windows Notepad to the more advanced OpenOffice Writer includes a feature to search its text. There are other reasons to implement a textbox search feature and hopefuly this article will show you how. The normal textbox control in .NET does not have any find/search capabilities that I could find. So I ended up having to create my own searching code for a control I made along time ago and thought it could be useful for someone if I posted it.

Visual Basic.NET, VB 2008/2010/2013

Since DotNET doesn’t have built-in support for searching/finding text, you will need to make your own code. This is a snippet I originally made for a more advanced textbox control called: ‘csTextBoxX’. There are many other features available as well if you want to take a look at it here and with an example here.


Below is the Function I made…

'
'I’m sure this Function can be more refined if you wanted to do it.
'
Public Overloads Function SearchText(ByVal textToFind As String, Optional ByVal startPosition As Integer = 0, Optional ByVal endPosition As Integer = 0, Optional ByVal highlightText As Boolean = True, Optional ByVal matchCase As Boolean = False) As Integer
'
'Contains the return value of the search. IT it returns -1, then a match was not found.
Dim i As Integer

If endPosition < 1 Then

  If Not matchCase Then

    textToFind = textToFind.ToLower

  Dim temp As String = Textbox.Text.ToLower

    i = temp.IndexOf(textToFind, startPosition, Me.Text.Length)

  Else

    i = Textbox.Text.IndexOf(textToFind, startPosition, Me.Text.Length)

  End If

Else

  If matchCase = False Then

      textToFind = textToFind.ToLower

      Dim temp As String = Textbox.Text.ToLower

      i = temp.IndexOf(textToFind, startPosition, endPosition)

  Else

      i = Textbox.Text.IndexOf(textToFind, startPosition, endPosition)

  End If

End If

If i > -1 Then

  If highlightText Then

    Textbox.Focus()

    Textbox.SelectionStart = i

    Textbox.SelectionLength = textToFind.Length

  End If

End If
'
'Returns the position the text was found at, otherwise it will report -1, which means that the search string was not found.
  Return i

End Function

Unfortunately my Function may not be easy to understand but It does work. You can also go through it and refine/optimize it where you can. I’m sure there are areas that need some attention.

The function supports Match Casing, No Match Casing, and Search at a specific Starting and/or Ending position within the text. There is also an option whether to Highlight the text that was found or not. Check out the example below to see it in use…

'
'Will contain the position where the text was found.
  Dim textFound As Integer
'
'Find the first occurance of the text “box” at the beginning of the textbox through to the end of the textbox. Selected True to Highlight the text that was found and disabled searching text for the same casing.
  textFound = SearchText(“box”, 0, Textbox.TextLength, True, False)
'
'Display a message with the starting position of the text if it was found. If a -1 is returned then it means it did Not find the specified text.
  MsgBox(textFound.ToString)

The example above starts searching at the beginning of the text and will go to the end of the text to find a match. It was set to highlight the text if found and not to worry about matching the text casing.


That’s all I guess. As a side note, I was almost sure that Visual Basic 2005 (.NET 2.0 and Newer) added a find/search feature in the textbox control but I couldn’t find it. So you can use this function for VB 2005, Visual Basic 2008, VB 2010, and newer. Have fun 🙂

Jason

Revised: 2014

Check if a String contains a specified String in VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (7 votes, average: 5.00 out of 5)
 

This code will search a string to check if it contains a specific string. Works for VB.NET, VB 2008, Visual Basic 2010, and Newer.

This code Is case sensitive.

        '
        'Check if a String contains a specified string. This code IS Case Sensitive.
        'Check the second set of codes below for Non-Case Sensitive.
        '
        Dim str As String = "This string has some text"

        If str.Contains("string") Then

            MessageBox.Show("Yeps, that string is in here!")

        Else

            MessageBox.Show("No, the string is not in here!")

        End If

 

The code here is Not case sensitive.

        '
        'This code is NOT case sensitive.
        '
        Dim str As String = "This STring has some text"

        If LCase(str).Contains(LCase("striNg")) Then

            MessageBox.Show("Yeps, that string is in here!")

        Else

            MessageBox.Show("No, the string is not in here!")

        End If

Minor Revision: 2015

Find/Search for a String in a Listbox/Combobox control by using VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

This will search for a String value in the Listbox or Combobox items in Visual Basic.NET, VB 2005 and Higher

            '
            'TxtItem.Text would be a textbox control whose text string will be searched. And of course, lstItems
            'is a Listbox control or you can make it a Combobox control. The code below shows searching
            'for a  partial/exact string at either the beginning of the control or at a specified Index value.
            '
            'Search a Listbox Control for a exact string
            lstItems.SelectedIndex = lstItems.FindStringExact(txtItem.Text)
            '
            'Search a Listbox Control for a partial string
            lstItems.SelectedIndex = lstItems.FindString(txtItem.Text)