Private Sub moCombo_KeyPress(KeyAscii As Integer)
'Remarks : 1. When the user types something into the text
' portion, move to the first
' list entry which begins with the displayed
' text.
' 2. Not all keys trigger this event. In
' particular -
'
' {Delete} ... triggers KeyDown but not
' KeyPress
' {BackSpace} ... triggers KeyPress but not
' KeyDown
'
' 3. This code was originally in the Change()
' event, but confusing
' interactions kept occurring (list index was
' being set to -1 by Windows)
'
Dim sSearchOn As String _
'current string to search on
On Error Resume Next
'These keys can trigger this event, but are of no
' interest to us ...
If KeyAscii = vbKeyReturn Or KeyAscii = vbKeyTab Then
GoTo Bye_moCombo_KeyPress _
'we don't want to know
End If
'So a content-modifying key was entered; decide what to
' search on...
sSearchOn = ResultingText(KeyAscii)
'Perform the search; if it resulted in a move, cancel the
' key input...
If KeyAscii = vbKeyBack Then _
'prevent re-finding original !
If SearchOn(sSearchOn, CB_FINDSTRINGEXACT) = True Then _
KeyAscii = 0
Else
If SearchOn(sSearchOn, CB_FINDSTRING) = True Then _
KeyAscii = 0
End If
Bye_moCombo_KeyPress:
Exit Sub
End Sub
Private Function SearchOn(ByVal sStartText$, ByVal _
iMatchType%) As Boolean
'Purpose : Searches the combo and relocates if a match is
' found
'
'Returns : Whether a move occurred - True or False
'
'Remarks :
'
Dim mOriginalIndex As Long
Dim mNewIndex As Long
Dim yMoveOccurred As Boolean
On Error Resume Next
mOriginalIndex = moCombo.ListIndex
yMoveOccurred = False
'Determine our start index for searching...
mOriginalIndex = mOriginalIndex - 1
If mOriginalIndex < -1 Then mOriginalIndex = -1
'Perform our search...
With moCombo
If Len(sStartText) > 0 Then _
'if it contains any text
mNewIndex = MatchingRow(sStartText, mOriginalIndex, _
iMatchType)
If mNewIndex <> -1 Then _
'match found
SendMessageByString .hwnd, CB_SETCURSEL, mNewIndex, _
0 'set list index
.SelStart = Len(sStartText)
.SelLength = Len(.Text) - Len(sStartText) _
'select the bit AFTER the match
yMoveOccurred = True
End If
Else _
'contains no text
SendMessageByString .hwnd, CB_SETCURSEL, -1, 0 _
'set list index to -1
If mOriginalIndex <> -1 Then yMoveOccurred = True
End If
End With
SearchOn = yMoveOccurred
End Function
|