一开始combox控件32700行查找数据用时63毫秒
换API方法用时2.7毫秒,提高速度22倍
换分二法数组1000万行搜索只要3.745微秒,效率提高了518.3万倍
源码资源下载:https://download.csdn.net/download/xiaoyao961/11229888
我的资源列表:https://me.csdn.net/download/xiaoyao961/
手机访问CSDN作者页面,没办法打开他的资源页,CSDN总是漏洞百出。
Combo1控件有32700个列表项,查找第32500项
用时40.5266毫秒-函数2-查找Combo1,位置32499
用时63.4764毫秒-函数1-查找Combo1,位置32499
用时2.7164毫秒-API-查找Combo1,位置32499
Function ComboxExists(ByVal Hwnd As Long, ByVal FindStringAll As String) As Boolean
'查找Combox控件某行是否包含字符串,CB_FINDSTRINGEXACT=&H158
ComboxExists = SendMessageByString(ByVal Hwnd, ByVal CB_FINDSTRINGEXACT, ByVal -1&, ByVal FindStringAll) <> -1
End Function
VB6,VB.NET,高速查找COMBOX列表项。用combox1.list(x)的方法,只能查询32767行,多了就不行了。
Integer 2 个字节 -32,768 到 32,767。用API可以操作几十万行无压力。
查找10万项中的95000行也只是用了7毫秒。
函数1,2是另一个网友的博客所写的方法:对比十二年前的代码,发现了点有意思的东西 - 两天打鱼三天晒网 - CSDN博客
https://blog.csdn.net/sysdzw/article/details/90691174
Public Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Public Const CB_FINDSTRINGEXACT As Integer = &H158 '在ComboBox中精确查找
Function FindInCombox(ByRef cbo As ComboBox, str1 As String) As Long
'方法1
FindInCombox = -1
Dim sameitem
sameitem = 0
Do While sameitem <> cbo.ListCount
If cbo.List(sameitem) = str1 Then
FindInCombox = sameitem
Exit Function
End If
sameitem = sameitem + 1
Loop
End Function
Function FindInCombox2(cbo As ComboBox, ByVal str1 As String) As Long
'方法2:
FindInCombox2 = -1
Dim i%
For i = 0 To cbo.ListCount - 1
If cbo.List(i) = str1 Then
FindInCombox2 = i
Exit Function
End If
Next
End Function
'方法3,用ComboX控件本身的方示搜索字符串
Function CbFind(ByVal Hwnd As Long, ByVal FindStringAll As String) As Long
CbFind = SendMessageByString(ByVal Hwnd, ByVal CB_FINDSTRINGEXACT, ByVal -1&, ByVal FindStringAll)
End Function