|
-
Oct 21st, 2001, 03:25 PM
#1
Thread Starter
Hyperactive Member
How do I use LB_FINDSTRING
I am doing a little application that needs the ability to search a listbox and I found LB_FINDSTRING might help me do so. I also would like to eliminate anything that does not partain to the search criteria which would be entered into a textbox. And as I back space I would like any thing that was eliminated to be put back into the listbox.
For example:
I have 306 Items in the list box. Two of wich are "earthmate" and "eartha".
If I type "earth" in the text box it will display both earthmate and eartha. However, if I type "earthm" only "earthmate" will be displayed in the listbox. What I want to do is delete the "m" from the textbox and get both eartha and earthmate displayed in the listbox again. Also if I clear the contents of the textbox I would like the listbox to be repopulated.
If you have any questions feel free to ask.
-
Oct 21st, 2001, 04:04 PM
#2
It works with a null-terminated string
Using your example finding all the 'earth' entries.
PS: LB_FINDSTRING is not case sensitive.
Code:
Const LB_ERR = (-1)
Const LB_FINDSTRING = &H18F
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As _
Long, ByVal wParam As Long, ByVal lParam As String) As Long
' note the change in the SendMessage declaration
Dim MySels() as integer
dim tmp as string
dim retval as long
tmp = "earth" & chr(0)
retval = 0
where = 0
Do While retval <> LB_ERR
retval = SendMessage(List1.hWnd,LB_FINDSTRING,where,tmp)
if retval <> LB_ERR then
Redim Preserve MYSels(Ubound(MySels) + 1)
MySels(Ubound(MYSels) ) = retval
where = retval +1
End if
Loop
The array MySels() will have the index values of each item that starts with 'earth'.
-
Oct 21st, 2001, 07:16 PM
#3
PowerPoster
just changed the file a bit to make it select the closer text in textbox.
-
Oct 21st, 2001, 08:51 PM
#4
Thread Starter
Hyperactive Member
That worked great now I just have one more problem, how can I make the selection go to the top of the list box??
-
Oct 21st, 2001, 08:57 PM
#5
Thread Starter
Hyperactive Member
nevermind....I figured it out.
-
Oct 21st, 2001, 09:31 PM
#6
PowerPoster
Hi
I have made a few changes to Jim's code so that u can store all the values in one list and only display the matches in another. It may or may not be what u want but anyways....
FORM - TEXT1, LIST1 (invisible) LIST2
VB Code:
Const LB_ERR = (-1)
Const LB_FINDSTRING = &H18F
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As _
Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Sub Form_load()
Me.Show
List1.Visible = False
'Sample data
For x = 1 To 4
List1.AddItem Chr$(x + 64)
Next
List1.AddItem "earthmate"
For x = 1 To 4
List1.AddItem Chr$(x + 68)
Next
List1.AddItem "earth"
For x = 1 To 4
List1.AddItem Chr$(x + 72)
Next
Repopulate
End Sub
Private Sub Text1_Change()
If Len(Text1.Text) = 0 Then
Repopulate
Else
FindMatches Trim$(Text1.Text)
End If
End Sub
Private Sub FindMatches(ByVal SearchText As String)
Dim StopRepeat As Long 'Check to prevent endless looping
Dim UpBound As Long 'upper bound of matches
Dim x As Long 'counter
Dim MySels() As Integer
Dim retval As Long
retval = 0
Where = 0
StopRepeat = 0
UpBound = -1
Do While retval <> LB_ERR
retval = SendMessage(List1.hwnd, LB_FINDSTRING, Where, SearchText)
If retval <= StopRepeat Then retval = LB_ERR
If retval <> LB_ERR Then
UpBound = UpBound + 1
ReDim Preserve MySels(UpBound)
MySels(UpBound) = retval
If UpBound = 0 Then StopRepeat = retval
Where = retval
End If
Loop
'Fill second list with matches
With List2
.Clear
If Where > 0 Then
For x = 0 To UBound(MySels)
.AddItem List1.List(MySels(x))
Next
End If
If .ListCount > 0 Then .ListIndex = 0
End With
End Sub
Private Sub Repopulate()
'No search term so refill
With List2
.Clear
For x = 0 To List1.ListCount - 1
.AddItem List1.List(x)
Next
If .ListCount > 0 Then .ListIndex = 0
End With
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
|