Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在vb.net的CheckedListBox中将选中项存储在数组中_Arrays_Vb.net_Checkedlistbox - Fatal编程技术网

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))
    显示项目的文本

  • 因此,将生成如下输出:(假设列表中有4项,其中选中了2项和3项)

    Checked Items: Item 2 = Apple
    Checked Items: Item 3 = Banana