'form1里的代码 Option ExplicitPrivate Sub cmdClose_Click() End End Sub Private Sub Form_Load() Dim i As Integer Dim k As String For i = 1 To 22 k = Chr(i + 64) & CStr(i) ListBox1.AddItem k Next i miNumKeys = 0 End SubPrivate Sub ListBox1_Click() Dim szListText As String Dim iListIndex As Integer On Error Resume Next If ListBox1.ListIndex >= 0 And miCtrlFocus = LISTBOX_FOCUS Then iListIndex = ListBox1.ListIndex szListText = ListBox1.List(iListIndex) txtSearch.Text = szListText End If End SubPrivate Sub ListBox1_KeyDown(KeyCode As Integer, Shift As Integer) miCtrlFocus = LISTBOX_FOCUS miNumKeys = 0 End SubPrivate Sub txtSearch_Change() Dim szSrchText As String Dim iTxtLen As Integer Dim iListIndex As Integer Dim fReturn As Integer
On Error Resume Next If miCtrlFocus = TEXTBOX_FOCUS And mfKeepKey And Not mfScrolling Then iTxtLen = Len(txtSearch.Text) If iTxtLen Then miNumKeys = IIf(miNumKeys < iTxtLen, miNumKeys, iTxtLen) szSrchText = txtSearch.Text fReturn = SearchListBox(szSrchText, ListBox1, iListIndex)
mfScrolling = True If iListIndex = -1 Then ListBox1.ListIndex = -1 Else ListBox1.Selected(iListIndex) = True txtSearch = ListBox1.List(ListBox1.ListIndex) txtSearch.SelStart = miNumKeys txtSearch.SelLength = (Len(txtSearch.Text) - miNumKeys) End If mfScrolling = False End If End If End SubPrivate Sub txtSearch_GotFocus() miNumKeys = 0 txtSearch.SelStart = 0 txtSearch.SelLength = Len(txtSearch.Text) End SubPrivate Sub txtSearch_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = KEY_BACK Or KeyCode = KEY_DELETE Or KeyCode = KEY_CLEAR Then mfKeepKey = False If KeyCode = KEY_BACK Then ListBox1.ListIndex = -1 End If Else mfKeepKey = True End If End SubPrivate Sub txtSearch_KeyPress(KeyAscii As Integer) miCtrlFocus = TEXTBOX_FOCUS If mfKeepKey Then miNumKeys = Len(txtSearch.Text) + 1 End If End Sub
'模块里的代码 Option Explicit Public Const KEY_BACK = &H8 Public Const KEY_DELETE = &H2E Public Const KEY_CLEAR = &HC Public Const TEXTBOX_FOCUS = 1 Public Const LISTBOX_FOCUS = 2 Public miCtrlFocus As Integer Public miNumKeys As Integer Public mfScrolling As Integer Public mfKeepKey As Integer Function SearchListBox(ByVal szSearchText As String, lbScroll As ListBox, iListIndex As Integer) As Integer Const FOUND = 0 Const LT = -1 Const GT = 1
Dim iListStart As Integer Dim iListCount As Integer Dim iTxtLen As Integer Dim szListText As String Dim vCompResult Dim fFound As Integer Dim fDone As Integer fFound = False iTxtLen = Len(szSearchText) If iTxtLen > 0 And lbScroll.ListCount > 0 Then iListStart = lbScroll.ListIndex If iListStart = -1 Then iListStart = 0 iListIndex = iListStart iListCount = lbScroll.ListCount szListText = Left(lbScroll.List(iListStart), iTxtLen) fFound = CInt(StrComp(szSearchText, szListText, 1)) If fFound <> FOUND Then fDone = False If (fFound = LT) Then iListIndex = 0 fFound = False Else iListIndex = iListIndex + 1 End If Do While (iListIndex <= iListCount) And Not fDone szListText = Left(lbScroll.List(iListIndex), iTxtLen) vCompResult = StrComp(szSearchText, szListText, 1) If IsNull(vCompResult) Then iListIndex = -1 Exit Do End If fFound = (CInt(vCompResult) = FOUND) fDone = fFound Or (CInt(vCompResult) = -1) If Not fDone Then iListIndex = iListIndex + 1 End If Loop If Not fFound Then iListIndex = -1 End If End If End If SearchListBox = fFound End Function
http://www.china-askpro.com/msg18/qa81.shtml
Option ExplicitPrivate Sub cmdClose_Click()
End
End Sub
Private Sub Form_Load()
Dim i As Integer
Dim k As String For i = 1 To 22
k = Chr(i + 64) & CStr(i)
ListBox1.AddItem k
Next i
miNumKeys = 0
End SubPrivate Sub ListBox1_Click()
Dim szListText As String
Dim iListIndex As Integer
On Error Resume Next If ListBox1.ListIndex >= 0 And miCtrlFocus = LISTBOX_FOCUS Then
iListIndex = ListBox1.ListIndex
szListText = ListBox1.List(iListIndex)
txtSearch.Text = szListText
End If
End SubPrivate Sub ListBox1_KeyDown(KeyCode As Integer, Shift As Integer)
miCtrlFocus = LISTBOX_FOCUS
miNumKeys = 0
End SubPrivate Sub txtSearch_Change()
Dim szSrchText As String
Dim iTxtLen As Integer
Dim iListIndex As Integer
Dim fReturn As Integer
On Error Resume Next If miCtrlFocus = TEXTBOX_FOCUS And mfKeepKey And Not mfScrolling Then
iTxtLen = Len(txtSearch.Text)
If iTxtLen Then
miNumKeys = IIf(miNumKeys < iTxtLen, miNumKeys, iTxtLen)
szSrchText = txtSearch.Text
fReturn = SearchListBox(szSrchText, ListBox1, iListIndex)
mfScrolling = True
If iListIndex = -1 Then
ListBox1.ListIndex = -1
Else
ListBox1.Selected(iListIndex) = True
txtSearch = ListBox1.List(ListBox1.ListIndex)
txtSearch.SelStart = miNumKeys
txtSearch.SelLength = (Len(txtSearch.Text) - miNumKeys)
End If
mfScrolling = False
End If
End If
End SubPrivate Sub txtSearch_GotFocus()
miNumKeys = 0
txtSearch.SelStart = 0
txtSearch.SelLength = Len(txtSearch.Text)
End SubPrivate Sub txtSearch_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = KEY_BACK Or KeyCode = KEY_DELETE Or KeyCode = KEY_CLEAR Then
mfKeepKey = False
If KeyCode = KEY_BACK Then
ListBox1.ListIndex = -1
End If
Else
mfKeepKey = True
End If
End SubPrivate Sub txtSearch_KeyPress(KeyAscii As Integer)
miCtrlFocus = TEXTBOX_FOCUS
If mfKeepKey Then
miNumKeys = Len(txtSearch.Text) + 1
End If
End Sub
Option Explicit Public Const KEY_BACK = &H8
Public Const KEY_DELETE = &H2E
Public Const KEY_CLEAR = &HC Public Const TEXTBOX_FOCUS = 1
Public Const LISTBOX_FOCUS = 2 Public miCtrlFocus As Integer
Public miNumKeys As Integer
Public mfScrolling As Integer
Public mfKeepKey As Integer
Function SearchListBox(ByVal szSearchText As String, lbScroll As ListBox, iListIndex As Integer) As Integer
Const FOUND = 0
Const LT = -1
Const GT = 1
Dim iListStart As Integer
Dim iListCount As Integer
Dim iTxtLen As Integer
Dim szListText As String
Dim vCompResult
Dim fFound As Integer
Dim fDone As Integer fFound = False
iTxtLen = Len(szSearchText) If iTxtLen > 0 And lbScroll.ListCount > 0 Then
iListStart = lbScroll.ListIndex
If iListStart = -1 Then iListStart = 0
iListIndex = iListStart
iListCount = lbScroll.ListCount
szListText = Left(lbScroll.List(iListStart), iTxtLen) fFound = CInt(StrComp(szSearchText, szListText, 1)) If fFound <> FOUND Then
fDone = False If (fFound = LT) Then
iListIndex = 0
fFound = False
Else
iListIndex = iListIndex + 1
End If Do While (iListIndex <= iListCount) And Not fDone
szListText = Left(lbScroll.List(iListIndex), iTxtLen)
vCompResult = StrComp(szSearchText, szListText, 1)
If IsNull(vCompResult) Then
iListIndex = -1
Exit Do
End If
fFound = (CInt(vCompResult) = FOUND)
fDone = fFound Or (CInt(vCompResult) = -1)
If Not fDone Then
iListIndex = iListIndex + 1
End If
Loop If Not fFound Then
iListIndex = -1
End If
End If
End If SearchListBox = fFound
End Function
txtSearchListBox1