List1.ListCount 可得到List1共有几个item 用 For i = 0 To List1.ListCount - 1 If List1.List(i) = "aaa" Then Exit For End Next If i >= List1.ListCount Then msgbox "你要的aaa项目不存在 ~~~~" End If
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 Const LB_FINDSTRINGEXACT = &H1A2Private Sub Command1_Click() Dim lret As Long Dim fnd_str As String
fnd_str = "test"
lret = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, -1, ByVal fnd_str) MsgBox lret End Sub
Private Sub Command1_Click() MsgBox existitem(List1, "66") End SubPrivate Sub Form_Load() List1.AddItem "55" List1.AddItem "66" End SubFunction existitem(lst As ListBox, str As String) As Boolean Dim i For i = 0 To lst.ListCount If lst.List(i) = str Then existitem = True Exit Function Else existitem = False Exit Function End If Next i End Function
用
For i = 0 To List1.ListCount - 1
If List1.List(i) = "aaa" Then
Exit For
End
Next
If i >= List1.ListCount Then
msgbox "你要的aaa项目不存在 ~~~~"
End If
如:5 表示有0-4个item项。
Private Const LB_FINDSTRINGEXACT = &H1A2Private Sub Command1_Click()
Dim lret As Long
Dim fnd_str As String
fnd_str = "test"
lret = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, -1, ByVal fnd_str)
MsgBox lret
End Sub
MsgBox existitem(List1, "66")
End SubPrivate Sub Form_Load()
List1.AddItem "55"
List1.AddItem "66"
End SubFunction existitem(lst As ListBox, str As String) As Boolean
Dim i
For i = 0 To lst.ListCount
If lst.List(i) = str Then
existitem = True
Exit Function
Else
existitem = False
Exit Function
End If
Next i
End Function