在我的一个表单中,我有一个ListBox(formListBox),其中包含字符串项列表。我想做的就是将列表框中的所有项目转移到一个集合中。到目前为止,我已经尝试了以下方法但没有成功:
Dim newItems As New ListBox.ObjectCollection(formListBox)
For Each item As String In newItems
myArrayList.addNewItem(item)
Next
执行此操作后,arraylist中的项目数返回为0。我有一种误解,就是“ ListBox.ObjectCollection(formListBox)”部分-我的印象是,它返回了ListBox的集合,但是我得到的结果建议不然。
试试这个
Dim arr1()
ReDim arr1(ListBox1.Items.Count - 1)
ListBox1.Items.CopyTo(arr1, 0)
或者
Dim ArrayItems() As String
'//ADD ITEMS INTO ARRAY
'//FIRST FIND HOW MANY ITEMS IN LISTBOX AND REDIM THE ARRAT FOR SIZE
ReDim ArrayItems(List1.ListCount)
'//NOW ADD ITEMS
For i = 1 To List1.ListCount
List1.ListIndex = i - 1
ArrayItems(i) = List1.Text
Next i
或者
使用LINQ
(From item As Object In yourListBox.ObjectCollection Select item.ToString()).ToArray()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句