Click to See Complete Forum and Search --> : listboxs
death
Nov 25th, 1999, 06:38 AM
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!
Keiko
Nov 25th, 1999, 08:05 AM
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
Compwiz
Nov 25th, 1999, 08:40 AM
Even better, try this: It searches to see if even a part of Text1 is in the listbox.
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 (http://wwp.icq.com/15743470) Add Me (http://wwp.icq.com/scripts/search.dll?to=15743470) ICQ Me (http://wwp.icq.com/scripts/contact.dll?msgto=15743470)
AIM: TomY10 (http://www.aol.com/aim/aim30.html)
PERL, JavaScript and VB Programmer
Keiko
Nov 25th, 1999, 09:41 AM
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
Gaurav
Nov 25th, 1999, 11:06 AM
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).]
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.