Arrays 如何在vb.net的CheckedListBox中将选中项存储在数组中
我将所有选中的项存储在一个字符串中,这对我来说非常合适,但我想将所有选中的项存储在一个带有名称的数组中Arrays 如何在vb.net的CheckedListBox中将选中项存储在数组中,arrays,vb.net,checkedlistbox,Arrays,Vb.net,Checkedlistbox,我将所有选中的项存储在一个字符串中,这对我来说非常合适,但我想将所有选中的项存储在一个带有名称的数组中 Dim i As Integer Dim ListItems As String ListItems = "Checked Items:" & ControlChars.CrLf For i = 0 To (ChkListForPrint.Items.Count - 1) If ChkListForPrint.GetIt
Dim i As Integer
Dim ListItems As String
ListItems = "Checked Items:" & ControlChars.CrLf
For i = 0 To (ChkListForPrint.Items.Count - 1)
If ChkListForPrint.GetItemChecked(i) = True Then
ListItems = ListItems & "Item " & (i + 1).ToString & " = " & ChkListForPrint.Items(i)("Name").ToString & ControlChars.CrLf
End If
Next
请帮忙 这应该可以
Dim ListItems as New List(Of String)
For i = 0 To (ChkListForPrint.Items.Count - 1)
If ChkListForPrint.GetItemChecked(i) = True Then
ListItems.Add(ChkListForPrint.Items(i)("Name").ToString)
End If
Next
如果您需要
CheckedItems
,那么为什么要使用项
?我建议使用CheckedItems
我对您的代码进行了一些修改,类似这样的内容可以帮助您:
Dim collection As New List(Of String)() ' collection to store check items
Dim ListItems As String = "Checked Items: " ' A prefix for any item
For i As Integer = 0 To (ChkListForPrint.CheckedItems.Count - 1) ' iterate on checked items
collection.Add(ListItems & "Item " & (ChkListForPrint.Items.IndexOf(ChkListForPrint.CheckedItems(i)) + 1).ToString & " = " & ChkListForPrint.GetItemText(ChkListForPrint.CheckedItems(i)).ToString) ' Add to collection
Next
在这里:
ChkListForPrint.Items.IndexOf(ChkListForPrint.CheckedItems(i))
将检查项目的索引ChkListForPrint.GetItemText(ChkListForPrint.CheckedItems(i))
显示项目的文本Checked Items: Item 2 = Apple
Checked Items: Item 3 = Banana