search a listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jarremw
    New Member
    • Jan 2008
    • 50

    search a listbox

    hello all
    i am writing a program for my company, am in no way a vb expert, probably barely a beginner, but anywho, i have a list box that pulls from a sql table, what i want to do is to be able to search the list box for a specific string from a textbox, when that text is found, it is selected.
    i know that there is a for loop involved, i just have no idea how to set it up, i searched google, but nothing really seems to work for vb 2005, any help?

    thanks in advance
  • jeffstl
    Recognized Expert Contributor
    • Feb 2008
    • 432

    #2
    Originally posted by jarremw
    hello all
    i am writing a program for my company, am in no way a vb expert, probably barely a beginner, but anywho, i have a list box that pulls from a sql table, what i want to do is to be able to search the list box for a specific string from a textbox, when that text is found, it is selected.
    i know that there is a for loop involved, i just have no idea how to set it up, i searched google, but nothing really seems to work for vb 2005, any help?

    thanks in advance

    Ok I am going to assume you are talking about populating a list box on a VB form for a .exe shell program and this is not web related? Also I will assume you are talking about VB 6.0 and not VB.NET? Since you didn't specify I was not sure.

    Based on those assumptions though this is your solution to get something selected in a List Box. (not a combo box right?). Set this code to run when you need it to select based on whats in a text box.

    Code:
        Dim myvalue As String
        myvalue = Text1.Text
        Dim i As Integer
        For i = 0 To List1.ListCount
            If List1.List(i) = myvalue Then
                List1.Selected(i) = True
            End If
        Next i

    Comment

    • jarremw
      New Member
      • Jan 2008
      • 50

      #3
      that didnt work, im using vb 2005, the one that comes with the visual studio, so i do not have the "list" operator.

      Comment

      • jeffstl
        Recognized Expert Contributor
        • Feb 2008
        • 432

        #4
        Originally posted by jarremw
        that didnt work, im using vb 2005, the one that comes with the visual studio, so i do not have the "list" operator.
        OK. So you are using VB.NET then.

        So that being the case, I am not sure off hand how to do that. I know with datagrids there is an ItemDataBound event that fires when data is bound to the control, within that event then you would check each row of data for whatever conditions.

        I am not sure about listboxes though, and a quick search on the web didn't reveal much other then the above example which was for vb6.0

        If there isn't a ItemDataBound like event for listboxes, then my guess is you would need a similiar loop like above in the sub Page_load, and loop during the read of the listbox data.

        Code:
        while ListBoxReader.Read
             ' check each row and compare to selected value to determine if marked selected
        End While
        Again im not sure that is the best way, because I havent worked with Listboxes yet in .NET.

        Comment

        • jarremw
          New Member
          • Jan 2008
          • 50

          #5
          i actually got it to work, i just used the listbox.findstr ing method....
          ill post an example on it when i have some time, but thanks for the help and the replies!

          Comment

          • jarremw
            New Member
            • Jan 2008
            • 50

            #6
            heres the code i used, i did a search in the microsoft help and found this, i modded it for my purposes....

            Code:
            Dim searchstring As String
                    searchstring = Me.TextBox1.Text
                    ' Set our intial index variable to -1.
                    Dim x As Integer = -1
                    ' If the search string is empty exit.
                    If searchstring.Length <> 0 Then
                        ' Loop through and find each item that matches the search string.
                        ' Retrieve the item based on the previous index found. Starts with -1 which searches start.
                        x = ListBox1.FindString(searchstring, -1)
                        If x <> -1 Then ListBox1.SetSelected(x, True)
                        If x <> ListBox1.FindString(searchstring, -1) Then MessageBox.Show("File Does Not Exist")
                    End If

            Comment

            Working...