|
-
Dec 3rd, 2000, 04:38 PM
#1
Thread Starter
Addicted Member
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!
-
Dec 3rd, 2000, 05:13 PM
#2
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
-
Dec 3rd, 2000, 06:34 PM
#3
Hyperactive Member
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 !!!!!
-
Dec 3rd, 2000, 08:28 PM
#4
Thread Starter
Addicted Member
Thanx
Thanks guys, they both work great. Private, how's the Napster project going?
-
Dec 3rd, 2000, 08:51 PM
#5
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
-
Jan 3rd, 2001, 05:57 PM
#6
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]
-
Jan 4th, 2001, 10:14 AM
#7
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
-
Jan 4th, 2001, 05:35 PM
#8
Fanatic Member
...Matthew...
Anything similar to your answer for listviews?
-
Jan 4th, 2001, 07:51 PM
#9
PowerPoster
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!
-
Jan 5th, 2001, 09:25 AM
#10
Fanatic Member
Thanks Chris....
But the problem with the list view (unlike the listbox)
is that the search is done only one the frist character typed
-
Jan 5th, 2001, 11:48 AM
#11
PowerPoster
Lafor, I never try this in ListView.
-
Jan 5th, 2001, 11:50 AM
#12
Fanatic Member
...
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|