Results 1 to 12 of 12

Thread: Using Lists

  1. #1

    Thread Starter
    Addicted Member WAcKeD's Avatar
    Join Date
    Aug 2000
    Posts
    211

    Question

    How would you go about searching through a list (List1) for a certain list item? Then, if the list item is in the list, it will display an error box, "User already present!"

    It would be a great help, thanks!
    Thankz,
    WAcKeD

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Assumes Text1(Textbox), Command Button(cmd1) and List(List1)

    Code:
    Private Sub cmd1_Click()
    Dim strSearch As String
    Dim matchFound As Boolean
    
      If Text1 <> "" Then
        strSearch = Text1.Text
        For x = 0 to List1.ListCount - 1
          If (List1.List(x) = strSearch) Then
            matchFound = True
          End If
        Next x
      End If
    
      If matchFound = False Then
        List1.AddItem strSearch
      End If
    
    End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    258
    I went a little farther . This will check any listbox for an item . if itsnot in the list it returns flase and adds the item , if it is in the list it responds flase . I figure a couple people may want it .

    in a mod

    Code:
    Option Explicit
    
    
    Public Function isinlist(tofind As String, lstbox As ListBox) As Boolean
    
    'Get the upper and lower of the list
    Dim i_Lower As Integer
    Dim i_Upper As Integer
    Dim i_tmpCounter
    Dim s_CurrentItem As String
    Dim b_matchFound  As Boolean
    
    
    isinlist = False
    i_Lower = 0 ' the lower
    i_Upper = lstbox.ListCount '- 1 ' the upper
    
    
    
    Select Case lstbox.ListCount
    
    
    
        Case 0
            lstbox.AddItem tofind
            isinlist = False
            Exit Function
            
            
    
    
        Case Else
    
            ' Now you need to loop through them all
            For i_tmpCounter = i_Lower To i_Upper
            's_CurrentItem = lstbox.List(i_tmpCounter)
            
    If (lstbox.List(i_tmpCounter) = tofind) Then
            b_matchFound = True
          End If
        Next i_tmpCounter
      
      End Select
      
      If b_matchFound = False Then
        lstbox.AddItem tofind
        isinlist = False
        Exit Function
        
        Else
        isinlist = True
        Exit Function
      End If
    
    
     
    End Function
    USAGE:

    Code:
    Private Sub Command1_Click()
    If isinlist(Text1.Text, Form1.List1) = True Then
    MsgBox "In list"
    Else: ' Do nothing
    End If
    
    End Sub
    Thats alls ,

    []P
    Visual Basic 6 SP4 on win98se

    QUIT THE RAT RACE BECAUSE YOUR MESSING THE WORLD UP !!!!!

  4. #4

    Thread Starter
    Addicted Member WAcKeD's Avatar
    Join Date
    Aug 2000
    Posts
    211

    Thumbs up Thanx

    Thanks guys, they both work great. Private, how's the Napster project going?
    Thankz,
    WAcKeD

  5. #5
    Guest

    A step further...with API

    Code:
    Private Declare Function SendMessage Lib "User32" _
    Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As _
    Integer, ByVal wParam As Integer, lParam As Any) As Long
    
    Const LB_FINDSTRING = &H18F
    
    Private Sub Text1_Change()
    
        List1.ListIndex = SendMessage(List1.hWnd, _
        LB_FINDSTRING, -1, ByVal Text1.Text)
    
    End Sub
    If you like that the found item list stays next to top of the ListBox control, try this:

    Code:
    Private Sub Text1_Change()
    
        On Error Resume Next
        List1.ListIndex = SendMessage(List1.hwnd, _
        LB_FINDSTRING, -1, ByVal Text1.Text)
        List1.TopIndex = List1.ListIndex - 1
    
    End Sub

  6. #6
    Guest

    Cool need embedded listbox search with lb_Findstring

    Is there a way to search a listbox for text that does not start off in the first char of a listbox instance...

    Example:

    I would like to find "456" within an instance of "123456" using the lb_findstring API call or similar API Call...

    I'm using the search in a change event of a dynamic search text box to search for an instance (while the user types the search string) ...


    Thanks in advance...


    Marc



    [Edited by mrmarshall on 01-05-2001 at 01:40 PM]

  7. #7
    Guest

    Thanks chris

    I thought that may be the case...

    My problem is that I have over 11000 records in this list box and I wanted to have a fast reply for the "type select" process that I perform...

    I may just load two or three listboxes and use the api to select the listboxs each starting with the string I'm searching for...

    Thanks anyways,
    Marc

  8. #8
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    617

    ...Matthew...

    Anything similar to your answer for listviews?

  9. #9
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    For ListView, you can use the following constant:

    Public Const LVM_FINDITEMA = (LVM_FIRST + 13)
    Public Const LVM_FINDITEMW = (LVM_FIRST + 83)

    To check the item juz use the SendMessage API will do and it should return the index of the listitem.

    Cheers!

  10. #10
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    617

    Thanks Chris....

    But the problem with the list view (unlike the listbox)
    is that the search is done only one the frist character typed

  11. #11
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Lafor, I never try this in ListView.

  12. #12
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    617

    ...

    Thanks anyway...

    You pointed me in the right direction and that's what matters

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width