Visual Basic 2010列表框错误

时间:2012-10-15 05:10:35

标签: visual-studio-2010

这个错误是什么意思?抱歉,这是我第一次使用Visual Basic 2010,我不熟悉这种错误,我使用它来选择列表框中的所有文件,并尝试移动或复制到其他形式的另一个列表框。

  

错误1'ToArray'不是其成员   'System.Windows.Forms.ListBox.ObjectCollection'。

这是我使用的代码。

  

Private Sub Button1_Click_1(ByVal sender As System.Object,ByVal e As   System.EventArgs)处理Button1.Click

     

如果RadioButton1.Checked那么

        Dim itemsToMove = ListBox1.Items.ToArray()
        For Each item In itemsToMove
            Form2.lstP.Items.Add(item)
            ListBox1.Items.Remove(item)
        Next
        Form2.Show()
    End If

End Sub

有人可以帮我这个吗?

2 个答案:

答案 0 :(得分:0)

错误的含义是Listbox.ObjectCollection没有名为 ToArray 的方法或属性。因此,您无法在此集合上调用 ToArray 。目前尚不清楚为什么要这样做。

答案 1 :(得分:0)

无需将ListBox强制转换为数组。如果您需要知道列表中的项目数,可以执行以下操作

    Dim itemsToMove As Integer = ListBox1.Items.Count

否则该行代码Dim itemsToMove = ListBox1.Items.ToArray()不是必需的。你可以简单地使用

    For Each item In ListBox1
        Form2.lstP.Items.Add(item)
        ListBox1.Items.Remove(item)
    Next