在vb中如何用程序表达listbox中被选中的项目

2024-12-17 06:28:11
推荐回答(2个)
回答1:

For i = 1 To List1.ListItems.Count

'逐条判断是否被选中,如果选中,得到该项目
If List1.ListItems(i).Selected = True Then
strItem = List1.ListItems(i)
end if
next

回答2:

Dim s

Private Sub Form_Click()
Dim c As Long
c = GetSelCount()
If c = 0 Then
MsgBox "一个都没选中!"
Exit Sub
End If
If c = 1 Then
MsgBox "选中一个!"
Else
MsgBox "选中了多个!"
End If
End Sub

Private Sub Form_Load()
For i = 0 To 10
List1.AddItem "test" & CStr(i)
Next
End Sub
Function GetSelCount() As Long
s = ""
For i = 0 To List1.ListCount - 1
If List1.Selected(i) Then
s = s & " " & CStr(i)
End If
Next
s = Trim(s)
If s = "" Then
'一个都没选中
GetSelCount = 0
Else
s = Split(s)
GetSelCount = UBound(s) + 1
End If
End Function