如何使VB6列表计数适用于超过32767项的列表?
for k = 0 to List2.ListCount - 1
''do stuff
next
上面的代码效果很好,直到我的列表中有太多项目,然后列表计数变得全部搞乱并且变为负数。
我该如何解决这个问题?我可以将ListCount设为长数据类型吗?
答案 0 :(得分:3)
虽然VB6属性只是签名Integer
,但底层控件使用32位整数,因此您可以使用Windows API来操作列表:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const LB_GETCOUNT = &H18B
Private Const LB_GETTEXT = &H189
Private Sub Command1_Click()
Dim ix As Long
For ix = 0 To 40000
List1.AddItem "X" + Format(ix)
Next
End Sub
Private Sub Command2_Click()
Dim rc As Long
Dim wp As Long
Dim lp As Long
Dim s As String * 1000
' Get count of items into rc
rc = SendMessage(List1.hwnd, LB_GETCOUNT, 0, 0)
' Get the 39001th item
rc = SendMessageString(List1.hwnd, LB_GETTEXT, 39000, s)
MsgBox s
End Sub
说过切换到ListView可能仍然是一个好主意。