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!
Printable View
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!
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
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
USAGE: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
Thats alls ,Code:Private Sub Command1_Click()
If isinlist(Text1.Text, Form1.List1) = True Then
MsgBox "In list"
Else: ' Do nothing
End If
End Sub
[]P
Thanks guys, they both work great. Private, how's the Napster project going?
If you like that the found item list stays next to top of the ListBox control, try this: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
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
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]
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
Anything similar to your answer for listviews?
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!
But the problem with the list view (unlike the listbox)
is that the search is done only one the frist character typed
Lafor, I never try this in ListView.
Thanks anyway...
You pointed me in the right direction and that's what matters