看了看,加了一些注释,其实沉下心来,应该能看懂的 Form表单代码'有个小bug,在ListBox获得焦点时,任意按键,不能把按键相对应的值调出到txtSearch文本框,如果需要此功能自己另加 Option Explicit Dim iListIndex As Integer '这个由ListBox1_Click()和txtSearch_Change()种都定义了,可以设成全局变量 Private 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 Sub '只有ListBox1_KeyDown()事件发生后这个才能触发,只有这样miCtrlFocus = LISTBOX_FOCUS Private 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_GotFocus() '现加的 miCtrlFocus = LISTBOX_FOCUS miNumKeys = 0 End Sub'按任意键触发后,点击ListBox才有效 设计不好,换成上面ListBox1_GotFocus()事件 Private Sub ListBox1_KeyDown(KeyCode As Integer, Shift As Integer) miCtrlFocus = LISTBOX_FOCUS miNumKeys = 0 End SubPrivate Sub txtSearch_Change() Dim szSrchText As String '模块中函数SearchListBox()参数 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 'mfKeepKey除了特定的三个键,都是True;mfScrolling为整形时默认0,布尔型时默认False iTxtLen = Len(txtSearch.Text) If iTxtLen Then 'miNumKeys是你输入的字符个数 miNumKeys = IIf(miNumKeys < iTxtLen, miNumKeys, iTxtLen) szSrchText = txtSearch.Text fReturn = SearchListBox(szSrchText, ListBox1, iListIndex) '判断你输入的字符串,下面框中有没有相匹配的
mfScrolling = True If iListIndex = -1 Then ListBox1.ListIndex = -1 Else '选定你自动显示的又不是你输入的那部分字符,例如你输入a,直接变成了A1,那1就被选定,被你接下来输入的字符覆盖 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 End Sub '只接受下面三个键值 Private 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 Sub '除了上面三个键,按其他键时触发 Private 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 '十六进制的8,退格键的ASCII码 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 '这里应改成Boolean,因为它后面都按Boolean 型处理的。还有下面那个,同理 'Public mfScrolling As Boolean Public mfKeepKey As Integer 'Public mfKeepKey As Boolean' 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) '左取选定那行iTxtLen长度的字符串,iTxtLen是输入到文本框的字符串长度
If (fFound = LT) Then iListIndex = 0 fFound = False Else iListIndex = iListIndex + 1 End If '如果当前选择与txtSearch不相匹配,遍历找一个匹配的。-1时从0开始,1时从当前的下一个开始。找到匹配的循环就结束了 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 '如果没有匹配的,设置iListIndex = -1 If Not fFound Then iListIndex = -1 End If End If End If SearchListBox = fFound '返回值 End Function
帮帮我
Form表单代码'有个小bug,在ListBox获得焦点时,任意按键,不能把按键相对应的值调出到txtSearch文本框,如果需要此功能自己另加
Option Explicit
Dim iListIndex As Integer '这个由ListBox1_Click()和txtSearch_Change()种都定义了,可以设成全局变量
Private 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 Sub
'只有ListBox1_KeyDown()事件发生后这个才能触发,只有这样miCtrlFocus = LISTBOX_FOCUS
Private 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_GotFocus() '现加的
miCtrlFocus = LISTBOX_FOCUS
miNumKeys = 0
End Sub'按任意键触发后,点击ListBox才有效 设计不好,换成上面ListBox1_GotFocus()事件
Private Sub ListBox1_KeyDown(KeyCode As Integer, Shift As Integer)
miCtrlFocus = LISTBOX_FOCUS
miNumKeys = 0
End SubPrivate Sub txtSearch_Change()
Dim szSrchText As String '模块中函数SearchListBox()参数
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
'mfKeepKey除了特定的三个键,都是True;mfScrolling为整形时默认0,布尔型时默认False
iTxtLen = Len(txtSearch.Text)
If iTxtLen Then 'miNumKeys是你输入的字符个数
miNumKeys = IIf(miNumKeys < iTxtLen, miNumKeys, iTxtLen)
szSrchText = txtSearch.Text
fReturn = SearchListBox(szSrchText, ListBox1, iListIndex) '判断你输入的字符串,下面框中有没有相匹配的
mfScrolling = True
If iListIndex = -1 Then
ListBox1.ListIndex = -1
Else '选定你自动显示的又不是你输入的那部分字符,例如你输入a,直接变成了A1,那1就被选定,被你接下来输入的字符覆盖
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
End Sub
'只接受下面三个键值
Private 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 Sub
'除了上面三个键,按其他键时触发
Private 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 '十六进制的8,退格键的ASCII码
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 '这里应改成Boolean,因为它后面都按Boolean 型处理的。还有下面那个,同理
'Public mfScrolling As Boolean
Public mfKeepKey As Integer
'Public mfKeepKey As Boolean'
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) '左取选定那行iTxtLen长度的字符串,iTxtLen是输入到文本框的字符串长度
fFound = CInt(StrComp(szSearchText, szListText, 1)) 'StrComp()字符串比较,1代表执行一个按照原文的比较。返回值-1,0,1,null,分别对应<,=,>,null的情况
If fFound <> FOUND Then 'FOUND是0,常数
fDone = False
If (fFound = LT) Then
iListIndex = 0
fFound = False
Else
iListIndex = iListIndex + 1
End If
'如果当前选择与txtSearch不相匹配,遍历找一个匹配的。-1时从0开始,1时从当前的下一个开始。找到匹配的循环就结束了
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
'如果没有匹配的,设置iListIndex = -1
If Not fFound Then
iListIndex = -1
End If
End If
End If SearchListBox = fFound '返回值
End Function