-
ListBox Question?
How do you search a listbox character by character and make the result the 1st position in the listbox. In other words, the user types "c" and the first listbox item that contains a "c" as the first character will be displayed. If the user types "ch", the first occurrence will be displayed in position 1.
Thanks,
Jeff
-
While you can code this, you could try the combo box in forms 2.0 library. As I recall it does this already, but I could be wrong.
-
That does work. But I'm confused on one thing.
When I try to enter a value that is not contained within the list, it won't let me continue entering data. I don't know if this is because of the settings or if it has something to do with the KeyPress event.
Any ideas?
thanks again,
Jeff
-
The following code works for a list box. The code would be a little bit simplier if you would use a combo box since you don't have to remember the last keystokes (you only need to send the Text property)
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 Declare Function GetTickCount _
Lib "kernel32" () As Long
Private Const LB_FINDSTRING = &H18F
Private Sub List1_KeyPress(KeyAscii As Integer)
Dim nCurrentTime As Long
Dim nRetVal As Long
Static nTime As Long
Static sTxt As String
nCurrentTime = GetTickCount
If nCurrentTime - nTime > 1000 Then
nTime = GetTickCount
sTxt = ""
End If
Select Case KeyAscii
Case vbKeyBack
If Len(sTxt) Then
sTxt = Left$(sTxt, Len(sTxt) - 1)
End If
Case Else
sTxt = sTxt & Chr$(KeyAscii)
End Select
If Len(sTxt) Then
nRetVal = SendMessage(List1.hwnd, LB_FINDSTRING, 0, ByVal sTxt)
List1.ListIndex = nRetVal
End If
Debug.Print sTxt
End Sub
Best regards