Results 1 to 6 of 6

Thread: list help

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    52

    list help

    how would i go about.. if text1 is in list1, don't add

    :\ If it's not, add!

    help please

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: list help

    You have 2 basic choices:
    1. Loop thru the list items from zero to .Listcount-1 and compare each .List(n) with the textbox text
    2. Use API and quickly determine if match can be found. Search forum for LB_FINDSTRINGEXACT for an example.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    52

    Re: list help

    Code:
    If text1.text = List2.ListIndex Then
    Exit Sub
    Else: List2.AddItem text1.text
    End If
    i'm not quite sure if i should be using list2.listindex or not. but yeah :\ help fix that?

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: list help

    Code:
    Dim lngIndex As Long
    Dim bFound As Boolean
    
    For lngIndex = 0 to List2.Listcount -1
        If Text1.Text =  List2.List(lngIndex) Then
            bFound = True
            Exit For
        End If
    Next
    
    If not bFound Then
        List2.AddItem Text1.Text
    Else
        MsgBox Text1.Text & "already added"
    End If

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    52

    Re: list help

    thanks worked like a charm

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: list help

    I'm glad that works for you but as LaVolpe suggested this is the faster way.

    Option Explicit
    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
                             ByVal wParam As Long, lParam As Any) As Long
    
    Private Const LB_FINDSTRINGEXACT = &H1A2
    
    Private Sub SomeSub()
    
        If  SendMessage(List2.hwnd, LB_FINDSTRINGEXACT, -1&, _
                                    ByVal Text1.Text) > -1& Then
            MsgBox Text1.Text & " already added"
        Else
            List2.AddItem Text1.Text 
        End If
    
    End Sub

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