【问题标题】:dividing listbox items into two parts and pasting them into two listbox将列表框项目分成两部分并将它们粘贴到两个列表框中
【发布时间】:2018-06-30 14:55:58
【问题描述】:

所以我想运行的代码是:

 Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim size2 As Integer = ListBox1.Items.Count / 2
        Dim size1 As Integer = ListBox1.Items.Count - size2

        ListBox2.Items.AddRange(ListBox1.Items.GetRange(0, size1))
        ListBox3.Items.AddRange(ListBox1.Items.GetRange(size1, size2))
    End Sub

我已导入 System.Linq 但错误发生 getrange is not a member of listbox.objectcollection

【问题讨论】:

  • 问自己两个问题。 1) 你为什么使用SelectedIndexChanged 事件?每次您在 ListBox1 中选择一个项目时都会执行此代码,这是您想要的吗? 2)如果项目数是奇数怎么办?

标签: vb.net


【解决方案1】:

在 Linq 中没有 GetRange() 扩展这样的东西。但是,List(Of T) 类具有 such a method。因此,您可以使用Cast(Of TResult) extension 将集合转换为IEnumerable(Of T),然后使用ToList() extension 将其转换为List(Of T)

Dim ItemsList As List(Of Object) = ListBox1.Items.Cast(Of Object).ToList()

ListBox2.Items.AddRange(ItemsList.GetRange(0, size1))
ListBox3.Items.AddRange(ItemsList.GetRange(size1, size2))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多