vb.net 如何在vb.net的CheckedListBox中将选中的项目存储在数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13393603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to store checked items in an array in CheckedListBox in vb.net
提问by gaurav somani
i'm storing all checked items in a string this works perfectly for me but i want to store all the checked items in an array with their names.
我将所有选中的项目存储在一个字符串中,这对我来说非常有用,但我想将所有选中的项目与它们的名称一起存储在一个数组中。
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
Please help!
请帮忙!
采纳答案by WozzeC
This should do it.
这应该做。
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
回答by NeverHopeless
If you need CheckedItemsthen why you are using Itemsinstead ? I would recommend to use CheckedItems.
如果您需要,CheckedItems那么您为什么要使用它Items?我建议使用CheckedItems.
I have modified your code a bit and something like this would help you:
我稍微修改了你的代码,这样的事情会帮助你:
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
Here:
这里:
ChkListForPrint.Items.IndexOf(ChkListForPrint.CheckedItems(i))will get the index of the item checked.ChkListForPrint.GetItemText(ChkListForPrint.CheckedItems(i))will display the text of the item.
ChkListForPrint.Items.IndexOf(ChkListForPrint.CheckedItems(i))将检查项目的索引。ChkListForPrint.GetItemText(ChkListForPrint.CheckedItems(i))将显示项目的文本。
So would generate output like: (Assume 4 items in list in which 2 and 3 item is checked)
因此会生成如下输出:(假设列表中有 4 个项目,其中检查了 2 和 3 项)
Checked Items: Item 2 = Apple
Checked Items: Item 3 = Banana

