none
Search through listbox items' name RRS feed

  • Question

  • Greetings.

    I want to find a way to search through listbox items' names and loop the procedure. There is a textbox for user to enter desired string to be search and there is a list box and a button to search.

    * I found a solution to listbox items search :

    Private Sub FindMyString(ByVal searchString As String)
       ' Ensure we have a proper string to search for.
       If searchString <> String.Empty Then
          ' Find the item in the list and store the index to the item.
          Dim index As Integer = listBox1.FindString(searchString)
          ' Determine if a valid index is returned. Select the item if it is valid.
          If index <> -1 Then
             listBox1.SetSelected(index, True)
          Else
             MessageBox.Show("The search string did not match any items in the ListBox")
          End If
       End If
    End Sub

    But how to loop it ?! I mean if user clicks the button once , program should find the first occurrence and if he clicks the button once more again , it should look for other possible occurrences till the end of the listbox.

    Monday, June 12, 2017 3:42 AM

Answers

  • Try the variant of FindString that takes the starting index:

    Private Sub FindMyString(ByVal searchString As String)
       If searchString <> String.Empty Then
          listBox1.SelectedIndex = listBox1.FindString(searchString, listBox1.SelectedIndex)
          If listBox1.SelectedIndex = -1 Then
             MessageBox.Show("The search string did not match any items in the ListBox")
          End If
       End If
    End Sub

    It will start from current item. After reaching the end it will restart from the beginning. To force this restart, you can add a handler for your textbox:

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            listBox1.SelectedIndex = -1
    End Sub

    Can be improved to distinguish between "Not found at all" and "No more items".

    See also FindStringExact.




    • Edited by Viorel_MVP Monday, June 12, 2017 6:06 AM
    • Marked as answer by Kevin993 Monday, June 12, 2017 6:14 AM
    Monday, June 12, 2017 5:42 AM

All replies

  • But how to loop it ?! I mean if user clicks the button once , program should find the first occurrence and if he clicks the button once more again , it should look for other possible occurrences till the end of the listbox.

    You aren't searching a listbox - you are searching the listbox items collection.  That collection has methods such as IndexOf and Contains that do not require you to code a loop.
    https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection%28v=vs.110%29.aspx

    If you want to use a loop then that loop should iterate over the collection.

    A better solution is to maintain your listbox items in a collection that suits the operations (such as searching) that you intend to perform.  That might be a .Net collection such as a List, or it might be something more structured such as a Table.  Then use the list box to display the string representation of that collection


    • Edited by Acamar Monday, June 12, 2017 3:49 AM fmt
    Monday, June 12, 2017 3:48 AM
  • Hi Kevin993,

    Your code would be like this:
    Private Sub FindMyString(ByVal searchString As String)
      ' Ensure we have a proper string to search for.
      If searchString <> String.Empty Then
        For idx As Integer = 0 To ListBox1.Items.Count - 1
    	  If (ListBox1.Items(idx) = searchString) Then
            MessageBox.Show("searchString [" & searchString & "] has been found in ListBox1.")
            Exit Sub
          End If
        Next
        MessageBox.Show("searchString [" & searchString & "] does not exist in ListBox1.")
       End If
    End Sub

    Ashidacchi

    Monday, June 12, 2017 5:01 AM
  • Hi Kevin993,

    According toy our description, I put one ListBox and Textbox control in the form, please refer to the code below.

    Private Sub Form12_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListBox1.Items.Add("Computer")
            ListBox1.Items.Add("Screen")
            ListBox1.Items.Add("Modem")
            ListBox1.Items.Add("Printer")
            ListBox1.Items.Add("Scanner")
            ListBox1.Items.Add("Sound Blaster")
    
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            Dim i As Integer = ListBox1.FindString(TextBox1.Text)
            ListBox1.SelectedIndex = i
            If TextBox1.Text = "" Then
                ListBox1.SelectedIndex = -1
            End If
    
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 12, 2017 5:36 AM
    Moderator
  • Try the variant of FindString that takes the starting index:

    Private Sub FindMyString(ByVal searchString As String)
       If searchString <> String.Empty Then
          listBox1.SelectedIndex = listBox1.FindString(searchString, listBox1.SelectedIndex)
          If listBox1.SelectedIndex = -1 Then
             MessageBox.Show("The search string did not match any items in the ListBox")
          End If
       End If
    End Sub

    It will start from current item. After reaching the end it will restart from the beginning. To force this restart, you can add a handler for your textbox:

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            listBox1.SelectedIndex = -1
    End Sub

    Can be improved to distinguish between "Not found at all" and "No more items".

    See also FindStringExact.




    • Edited by Viorel_MVP Monday, June 12, 2017 6:06 AM
    • Marked as answer by Kevin993 Monday, June 12, 2017 6:14 AM
    Monday, June 12, 2017 5:42 AM
  • There is a problem .

    If listbox"s item's name is a single word like "Hello", searching works fine. But if it is a word within a sentence which should be searched , it doesn't find it. I also tried findstringExact , but it is the same.

    Help me on this please

    Monday, June 12, 2017 10:43 AM
  • There is a problem .

    If listbox"s item's name is a single word like "Hello", searching works fine. But if it is a word within a sentence which should be searched , it doesn't find it. I also tried findstringExact , but it is the same.

    Help me on this please

    Hi

    You have marked as answer, so less people would open this thread to see that you have further questions. In a case like this, you should either start a new thread with your current updated code, or, refrain from marking as answer until thread is complete.


    Regards Les, Livingston, Scotland

    Monday, June 12, 2017 2:37 PM