combo1.sorted=true for i=0 to combo1.listcount-1 if left(combo1.list(i),1)="1" then combo1.listindex=i exit for end if next i上面的程序效率较低,改用折半查找法会快一些
或者向COMBOBOX发送CB_FINDSTRING消息,具体的自已看一下帮助吧。
我不记得以下代码是从那张光盘里找出来的,好像是《程序员大本营》里的 用两分法搜索列表框或组合框中的数据下面的函数能让你在一个已作升序排列的列表框或组合框中查找符合条件的数据。你可以实现精确查找或模糊查找。对这个程序稍微变一下,你就可以在一组已排好序的数组中查找符合条件的数据。该方法的主要好处是对于大量的数据的查找,速度非常之快。例如,一个列表框中有1024条数据,用此法查找只须重复十次,而用传统的方法则要重复512次;如果数据量翻倍,用此法的重复次数为11次,而传统的方法重复的次数也会翻倍。 Windows API提供了四个消息来实现上述的功能。对于列表框是:LB_FINDSTRING和LB_FINDSTRINGEXACT;对于组合框是:CB_FINDSTRING和CB_FINDSTRINGEXACT。但下面的程序则不分列表框和组合框,只要事先对其中的数据排序即可。 代码如下: Function FindExact(Control As Control, Searched As String, Optional StartingIndex As Variant) Dim I As Long, j As Long, k As Long FindExact = -1 I = Iif(IsMissing(StartingIndex), 0, StartingIndex) j = Control.ListCount - 1 Do If I > j Then Exit Function k = (I + j) / 2 Select Case StrComp(Control.List(k), Searched) Case 0: Exit Do Case -1: I = k + 1 Case 1: j = k - 1 End Select Loop Do While k > 0 If StrComp(Control.List(k - 1), Searched) Then Exit Do End If k = k - 1 Loop FindExact = k End FunctionFunction FindPartial(Control As Control, Searched As String, Optional StartingIndex As Variant) Dim I As Long, j As Long, k As Long, lun As Long FindPartial = -1 I = Iif(IsMissing(StartingIndex), 0, StartingIndex) j = Control.ListCount - 1 lun = Len(Searched) Do If I > j Then Exit Function k = (I + j) / 2 Select Case StrComp(Left(Control.List(k), lun), Searched) Case 0: Exit Do Case -1: I = k + 1 Case 1: j = k - 1 End Select Loop Do While k > 0 If StrComp(Left(Control.List(k - 1), lun), Searched) Then Exit Do End If k = k - 1 Loop FindPartial = k End Function'程序用法如下: 'Index = FindExact(Control, Searched[, StartingIndex]) 'Index = FindPartial(Control, Searched[, StartingIndex])'其中Control代表列表框或组合框的名称,Searched表示搜索条件,StartingIndex为可选参数,表示从哪里开始搜索。函数的返回值为符合条件的数据的索引值。如果未找到符合条件的数据,则返回值为-1。
另一篇 Combo的自动查询技术 Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Public Const CB_FINDSTRING = &H14C Private Sub Combo1_Change() Dim iStart As Integer Dim sString As String Static iLeftOff As Integer iStart = 1 iStart = Combo1.SelStart If iLeftOff <> 0 Then Combo1.SelStart = iLeftOff iStart = iLeftOff End If sString = CStr(Left(Combo1.Text, iStart)) Combo1.ListIndex = SendMessage(Combo1.hwnd,B_FINDSTRING, -1, ByVal CStr( Left( ombo1.Text, iStart)))
If Combo1.ListIndex = -1 Then iLeftOff = Len(sString) combo1.Text = sString End If Combo1.SelStart = iStart iLeftOff = 0 End Sub 静态变量 iLeftOff 指定了字符长度。
以下是实现你想要的功能的源代码:(本源代码还需要一个TextBox控件) Option ExplicitPrivate Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long Private Const CB_FINDSTRING = &H14C Private Const CB_SHOWDROPDOWN = &H14F Dim IsChange As Boolean Private Sub Form_Load() Form1.Show Text1.Move Combo1.Left + 30, Combo1.Top + 50, Text1.Width - 50, Combo1.Height - 70 IsChange = True End Sub Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer) Dim i As Long, txtStart As Long '用于实现Combo控件的自动下拉 SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, 1, ByVal 0&If IsChange Then '实现该功能的关键语句,实现自动查找符合项目 Combo1.ListIndex = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal CStr(Text1.Text)) End If Select Case KeyCode Case vbKeyReturn For i = 0 To Combo1.ListCount If Combo1.List(i) = Text1.Text Then Text1.Text = "" Exit Sub End If Next i If Combo1.ListIndex <> -1 Then txtStart = Text1.SelStart Text1.Text = Combo1.List(Combo1.ListIndex) Text1.SelStart = txtStart Text1.SelLength = Len(Text1.Text) - Text1.SelStart Else
Combo1.AddItem Text1.Text Text1.Text = "" End If Case vbKeyUp Combo1.ListIndex = IIf(Combo1.ListIndex - 1 >= 0, Combo1.ListIndex - 1, Combo1.ListIndex) IsChange = False Case vbKeyDown Combo1.ListIndex = IIf(Combo1.ListIndex + 1 < Combo1.ListCount, Combo1.ListIndex + 1, Combo1.ListIndex) IsChange = False End Select End Sub看看现在是否已经实现了你想要的功能了呢? see it?
for i=0 to combo1.listcount-1
if left(combo1.list(i),1)="1" then
combo1.listindex=i
exit for
end if
next i上面的程序效率较低,改用折半查找法会快一些
用两分法搜索列表框或组合框中的数据下面的函数能让你在一个已作升序排列的列表框或组合框中查找符合条件的数据。你可以实现精确查找或模糊查找。对这个程序稍微变一下,你就可以在一组已排好序的数组中查找符合条件的数据。该方法的主要好处是对于大量的数据的查找,速度非常之快。例如,一个列表框中有1024条数据,用此法查找只须重复十次,而用传统的方法则要重复512次;如果数据量翻倍,用此法的重复次数为11次,而传统的方法重复的次数也会翻倍。 Windows API提供了四个消息来实现上述的功能。对于列表框是:LB_FINDSTRING和LB_FINDSTRINGEXACT;对于组合框是:CB_FINDSTRING和CB_FINDSTRINGEXACT。但下面的程序则不分列表框和组合框,只要事先对其中的数据排序即可。 代码如下: Function FindExact(Control As Control, Searched As String, Optional StartingIndex As Variant) Dim I As Long, j As Long, k As Long FindExact = -1
I = Iif(IsMissing(StartingIndex), 0, StartingIndex)
j = Control.ListCount - 1
Do
If I > j Then Exit Function
k = (I + j) / 2
Select Case StrComp(Control.List(k), Searched)
Case 0: Exit Do
Case -1: I = k + 1
Case 1: j = k - 1
End Select
Loop Do While k > 0
If StrComp(Control.List(k - 1), Searched) Then
Exit Do
End If
k = k - 1
Loop
FindExact = k
End FunctionFunction FindPartial(Control As Control, Searched As String, Optional StartingIndex As Variant)
Dim I As Long, j As Long, k As Long, lun As Long FindPartial = -1
I = Iif(IsMissing(StartingIndex), 0, StartingIndex)
j = Control.ListCount - 1
lun = Len(Searched)
Do
If I > j Then Exit Function
k = (I + j) / 2
Select Case StrComp(Left(Control.List(k), lun), Searched)
Case 0: Exit Do
Case -1: I = k + 1
Case 1: j = k - 1
End Select
Loop Do While k > 0
If StrComp(Left(Control.List(k - 1), lun), Searched) Then
Exit Do
End If
k = k - 1
Loop
FindPartial = k
End Function'程序用法如下:
'Index = FindExact(Control, Searched[, StartingIndex])
'Index = FindPartial(Control, Searched[, StartingIndex])'其中Control代表列表框或组合框的名称,Searched表示搜索条件,StartingIndex为可选参数,表示从哪里开始搜索。函数的返回值为符合条件的数据的索引值。如果未找到符合条件的数据,则返回值为-1。
Combo的自动查询技术
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal
hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
As Long
Public Const CB_FINDSTRING = &H14C
Private Sub Combo1_Change()
Dim iStart As Integer
Dim sString As String
Static iLeftOff As Integer
iStart = 1
iStart = Combo1.SelStart
If iLeftOff <> 0 Then
Combo1.SelStart = iLeftOff
iStart = iLeftOff
End If
sString = CStr(Left(Combo1.Text, iStart))
Combo1.ListIndex = SendMessage(Combo1.hwnd,B_FINDSTRING, -1, ByVal CStr(
Left( ombo1.Text, iStart)))
If Combo1.ListIndex = -1 Then
iLeftOff = Len(sString)
combo1.Text = sString
End If
Combo1.SelStart = iStart
iLeftOff = 0
End Sub
静态变量 iLeftOff 指定了字符长度。
Option ExplicitPrivate Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
Private Const CB_FINDSTRING = &H14C
Private Const CB_SHOWDROPDOWN = &H14F
Dim IsChange As Boolean
Private Sub Form_Load()
Form1.Show
Text1.Move Combo1.Left + 30, Combo1.Top + 50, Text1.Width - 50, Combo1.Height - 70
IsChange = True
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim i As Long, txtStart As Long
'用于实现Combo控件的自动下拉
SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, 1, ByVal 0&If IsChange Then
'实现该功能的关键语句,实现自动查找符合项目
Combo1.ListIndex = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal CStr(Text1.Text))
End If
Select Case KeyCode
Case vbKeyReturn
For i = 0 To Combo1.ListCount
If Combo1.List(i) = Text1.Text Then Text1.Text = ""
Exit Sub
End If
Next i
If Combo1.ListIndex <> -1 Then
txtStart = Text1.SelStart Text1.Text = Combo1.List(Combo1.ListIndex)
Text1.SelStart = txtStart
Text1.SelLength = Len(Text1.Text) - Text1.SelStart
Else
Combo1.AddItem Text1.Text
Text1.Text = ""
End If
Case vbKeyUp
Combo1.ListIndex = IIf(Combo1.ListIndex - 1 >= 0, Combo1.ListIndex - 1, Combo1.ListIndex)
IsChange = False
Case vbKeyDown
Combo1.ListIndex = IIf(Combo1.ListIndex + 1 < Combo1.ListCount, Combo1.ListIndex + 1, Combo1.ListIndex)
IsChange = False
End Select
End Sub看看现在是否已经实现了你想要的功能了呢?
see it?