列表框中的选定项到数组 [英] Selected Items in a ListBox to an Array
本文介绍了列表框中的选定项到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我是VB.Net的NUBIE,但学习很快.我有一个listbox2设置为Selectionmode = Extended,需要将列表框中的选定项捕获到数组中,以便以后进行操作.我可以轻松地获得第一个选定的项目文本,但是当我遍历代码时,它会继续重复第一个文本来填充数组.
任何帮助或建议,将不胜感激.
Hi All,
Im a NUBIE to VB.Net but am learning quickly. I have a listbox2 that is set to Selectionmode = Extended and need to capture the selected items in the list box to an array for a later action. I can easily get the first selected items text but as I iterate through the code it continues to populate the array with a repeat of the first text.
Any help or advice would be greatly appreciated.
Dim RDFs = ListBox2.SelectedItems.Count
Dim RDFsArry(RDFs) As String
Dim index2 As Integer
index2 = 0
For Each selectedItems In ListBox2.SelectedIndex.ToString
RDFsArry(index2) = (ListBox2.SelectedItem).ToString
index2 = index2 + 1
Next
推荐答案
您使用的For Each
错误.它循环遍历一个集合,而ListBox2.SelectedIndex
不是一个集合.尝试使用ListBox2.SelectedIndices
.
You are using theFor Each
incorrectly. It loops through a collection andListBox2.SelectedIndex
is not a collection. Try usingListBox2.SelectedIndices
.
,假设项目在下面的代码中是字符串,但是您需要遍历列表框的选定索引
assuming that the items are string in the code below, but you need to go through the selectedindices of the listbox
For Each item As String In ListBox2.SelectedIndices
RDFsArray(index2) = item
index2 = index2 + 1
Next
不起作用,现在出现以下错误...
SelectedIndices''不是System.Windows.Controls.ListBox的成员
Did not work, getting the following error now...
SelectedIndices'' is not a member of System.Windows.Controls.ListBox
这篇关于列表框中的选定项到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文