Results 1 to 5 of 5

Thread: listboxs

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    67

    Post

    how do i get vb to look inside a list box to find a string of text and if it there then say bring up a message box
    ps I do know how to do the message box bit!

  2. #2
    Addicted Member
    Join Date
    Sep 1999
    Posts
    229

    Post


    Hi Death,

    You can try this.

    On your form, put a ListBox control named List1 and a TextBox control named Text1.

    -------------------

    Dim a As Integer

    With Me.List1

    .Clear
    .AddItem "apple"
    .AddItem "banana"
    .AddItem "mango"
    .AddItem "orange"

    For a = 0 To .ListCount - 1

    If (.List(a) = Me.Text1) Then

    Call MsgBox("You have " & .List(a), vbOKOnly + vbInformation, "Information")

    End If

    Next a

    End With


    Does it help ?

    Regards

  3. #3
    Hyperactive Member
    Join Date
    Jul 1999
    Location
    NY, USA
    Posts
    270

    Post

    Even better, try this: It searches to see if even a part of Text1 is in the listbox.

    Code:
    Private Sub Command1_Click()
        On Error Resume Next
        Dim a As Integer
        With List1
            For a = 0 To .ListCount - 1
                If InStr(1, .List(a), Text1.Text, vbTextCompare) <> 0 Then Call MsgBox(Text1.Text & " was found in the string: " & .List(a) & ", at line: " & a + 1, vbOKOnly + vbInformation, "Information")
            Next a
        End With
    End Sub
    
    Private Sub Form_Load()
        With Me.List1
            .Clear
            .AddItem "apple"
            .AddItem "banana"
            .AddItem "mango"
            .AddItem "orange"
        End With
    End Sub
    Lemme know if it works.

    ------------------
    Tom Young, 14 Year Old
    tyoung@stny.rr.com
    ICQ: 15743470 Add Me ICQ Me
    AIM: TomY10
    PERL, JavaScript and VB Programmer

  4. #4
    Addicted Member
    Join Date
    Sep 1999
    Posts
    229

    Post


    Hi Death, Compwiz

    It depends on the requirement.

    If you want to find the exact word, you can use mine, or as Compwiz has mentioned for part of text you may use Compwiz's code.

    Regards

  5. #5
    Lively Member
    Join Date
    May 1999
    Location
    India
    Posts
    97

    Post

    Hi,
    Well the ways suggesteddo work if you want a REALLY SLOW app', but if u want Performance this is the definative way to go!!!


    Private Sub URLListCombo_Change()

    Dim i As Long, j As Long
    Dim strPartial As String, strTotal As String

    'Prevent processing as a result of changes from code
    If m_bEditFromCode Then
    m_bEditFromCode = False
    Exit Sub
    End If

    With URLListCombo
    'Lookup list item matching text so far
    strPartial = .Text
    i = SendMessage(.hwnd, CB_FINDSTRING, -1, ByVal strPartial)
    'If match found, append unmatched characters
    If i <> CB_ERR Then
    'Get full text of matching list item
    strTotal = .List(i)
    'Compute number of unmatched characters
    j = Len(strTotal) - Len(strPartial)
    '
    If j <> 0 Then
    'Append unmatched characters to string
    m_bEditFromCode = True
    .SelText = Right$(strTotal, j)
    'Select unmatched characters
    .SelStart = Len(strPartial)
    .SelLength = j
    Else

    '*** Text box string exactly matches list item ***

    'Note: The ListIndex is still -1. If you want to
    'force the ListIndex to the matching item in the
    'list, uncomment the following line. Note that
    'PostMessage is required because Windows sets the
    'ListIndex back to -1 once the Change event returns.
    'Also note that the following line causes Windows to
    'select the entire text, which interferes if the
    'user wants to type additional characters.
    ' PostMessage Combo1.hwnd, CB_SETCURSEL, i, 0
    End If
    Else
    bNewURLEntered = True
    End If
    End With

    End Sub


    Mail me if u need an entire sample app i wrote, its something like the Address bar in IE, that looks ahead as u type in your stuff to auto complete. The logic to look for a string is the same.

    Hope this helps, Cheers
    ------------------
    Gaurav Mahindra
    gmahindra@extentia.com




    [This message has been edited by Gaurav (edited 11-26-1999).]

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