|
-
Aug 1st, 2000, 01:57 PM
#1
Thread Starter
Addicted Member
Im trying to set a timer, to highlight a "name" in my listbox and check if it matches the search text in text1.text, once it finds it, i want it to select the name, and im going to have it preform an action......
my problem is that i cant figure out how to have it scroll through the names list, highlight and move down, ive figured out how to determine what the selected text says (list1.text) but im still stuck, please help!
-
Aug 1st, 2000, 02:02 PM
#2
This will search a list and highlight the item.
Code:
'Used: Textbox, Listbox, CommandButton
'Its best if you search lowercase so when adding items:
'List1.additem Lcase(item)
'Textbox is the searchbox
Private Sub Command_Click()
On Error Resume Next
Screen.MousePointer = 11
Text1.Text = LCase(Text1.Text)
List1.ListIndex = -1
For i = 0 To List1.ListCount
List1.ListIndex = List1.ListIndex + 1
If List1 = Text1.text Then
Screen.MousePointer = 0
Exit Sub
End If
Next i
Screen.MousePointer = 0
End Sub
-
Aug 1st, 2000, 02:13 PM
#3
Thread Starter
Addicted Member
the On Error Resume Next is not working, i dont understand why, if there is no match, it errors here:
List1.ListIndex = List1.ListIndex + 1
thank you i appreciate your help!
-
Aug 1st, 2000, 02:27 PM
#4
Addicted Member
Originally posted by Smie
the On Error Resume Next is not working, i dont understand why, if there is no match, it errors here:
List1.ListIndex = List1.ListIndex + 1
thank you i appreciate your help!
If there's no match, then it goes one past the end of the list. Why on Error resume next doesn't work, i'm not sure, but that is the cause for the error.
Changing
Code:
For i = 0 To List1.ListCount
To...
Code:
For i=0 To List1.ListCount - 1
Should fix it.
If there's no match you should be able to set List1.ListIndex equal to -1 to deselect items in the listbox.
-
Aug 1st, 2000, 02:31 PM
#5
Addicted Member
Side Note:
Originally posted by Matthew Gates
This will search a list and highlight the item.
Code:
For i = 0 To List1.ListCount
List1.ListIndex = List1.ListIndex + 1
If List1 = Text1.text Then
Screen.MousePointer = 0
Exit Sub
End If
Next i
Won't this skip the first item in the list? I thought listbox Indexes started at 0, but by the time this does the comparison, the index will already be 1.
-
Aug 1st, 2000, 02:36 PM
#6
That's where the List1.ListIndex = -1 comes in.
The On Error Resume Next errors because when List1.ListIndex = List1.ListIndex + 1 comes to the end of the listbox, it can't plus one anymore, so it creates an error, and thats where the On Error Resume Next comes in.
-
Aug 1st, 2000, 02:53 PM
#7
Thread Starter
Addicted Member
ok, thank you for all your help, now after it finds a match, how would i make the listbox select the match?
-
Aug 1st, 2000, 02:59 PM
#8
Did you try it?
Code:
Private Sub Command_Click()
On Error Resume Next 'Skip error
Screen.MousePointer = 11 'Hourglass
Text1.Text = LCase(Text1.Text) 'Lowercase Search string
List1.ListIndex = -1 'Select negative number so it starts at 0 when searching
For i = 0 To List1.ListCount 'count the listbox
List1.ListIndex = List1.ListIndex + 1 'highlight and search listbox
If List1 = Text1.text Then 'If List1 = Searchstring Then...
Screen.MousePointer = 0 'Arrow
Exit Sub 'Stop leaving highlighted item
End If 'End the If
Next i 'Loop to next item until a search it found
Screen.MousePointer = 0 'If search wasn't found, the hourglass will stay but we want it back to the arrow
List1.ListIndex = -1 'unhighlight the list if nothing was found
End Sub
It should highlight the found item. That is the code I use for this directory searcher I made and it works very well.
-
Aug 1st, 2000, 03:06 PM
#9
Thread Starter
Addicted Member
Thanks a bunch, it worked!, but just as one final question, how would i have just a separate command button highlight say, the 5th object in the list? like you click the button and it selects the 5th object on the list.....i really appreciate all of your help today, thank you!
-
Aug 1st, 2000, 03:10 PM
#10
To highlight any item (say the 5th item):
Code:
List1.ListIndex = 5
You could also highlight the name:
-
Aug 1st, 2000, 05:50 PM
#11
Try this.
Code:
Private Sub Command1_Click()
'Change strTemp to the text you want to search for
strTemp = "MyTextHere"
For i = 0 To List1.ListCount - 1
List1.ListIndex = i
If UCase(List1.Text) = UCase(strTemp) Then Exit For Else List1.ListIndex = -1
Next i
End Sub
-
Aug 1st, 2000, 09:10 PM
#12
If you're list is going to be fairly long, you'll find the API a faster way to search the listbox, i.e.
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_FINDSTRING = &H18F ' Partial Match
Private Const LB_FINDSTRINGEXACT = &H1A2 ' Exact Match
Private Sub Text1_Change()
Dim sFind As String
sFind = Text1
List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, 0&, ByVal sFind)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|