Vb.net 从列表中移动一个项目,并将其从上一个列表中删除,对于每个项目都是相同的

Vb.net 从列表中移动一个项目,并将其从上一个列表中删除,对于每个项目都是相同的,vb.net,Vb.net,很抱歉,我在form1中有三个链接列表,在form2中有三个链接列表(名称相同),我想将form1链接列表上的特定选定链接项传递到form2的链接列表 For Each lists In {frm2.lst1, frm2.lst2, frm2.lst3} lists.Items.Add(DirectCast(Controls(lists.Name), ListBox).SelectedItem) 'this is supposed to add the

很抱歉,我在form1中有三个链接列表,在form2中有三个链接列表(名称相同),我想将form1链接列表上的特定选定链接项传递到form2的链接列表

    For Each lists In {frm2.lst1, frm2.lst2, frm2.lst3}
            lists.Items.Add(DirectCast(Controls(lists.Name), ListBox).SelectedItem) 
    'this is supposed to add the linked items on the form1 (DirectCast(Controls(lists.Name), ListBox).SelectedItem) to the linked lists on form2
                DirectCast(Controls(lists.Name), ListBox).Items.RemoveAt(DirectCast(Controls(lists.Name), ListBox).SelectedIndex)
'this is supposed to removes the selected linked items on the form1 
            Next
如果我这样做,它会起作用:

Dim a = lst1.selectedindex
    For Each lists In {frm2.lst1, frm2.lst2, frm2.lst3}
     lists.Items.Add(DirectCast(Controls(lists.Name), ListBox).SelectedItem) 
    Next
    For Each lists In {frm2.lst1, frm2.lst2, frm2.lst3}
    DirectCast(Controls(lists.Name), ListBox).Items.RemoveAt(a)
    Next

但是我想知道如何使每一个都相同有一个更好的方法,IMHO:

创建一个新表单,在其上放置4个列表框(名为listbox1、2、3和4),然后将此代码粘贴到:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim d As New DataTable
    d.Columns.Add("ShowText")
    d.Columns.Add("WhichList", GetType(Integer))

    d.Rows.Add("I started in LB1", 1)
    d.Rows.Add("I started in LB1 too", 1)
    d.Rows.Add("I started in LB1 also", 1)
    d.Rows.Add("I started in LB2", 2)
    d.Rows.Add("I started in LB2 too", 2)
    d.Rows.Add("I started in LB2 also", 2)
    d.Rows.Add("I started in LB3", 3)
    d.Rows.Add("I started in LB3 too", 3)
    d.Rows.Add("I started in LB3 also", 3)


    ListBox1.DataSource = New DataView(d) With {.RowFilter = "[WhichList] = 1"}
    ListBox1.DisplayMember = "ShowText"
    ListBox2.DataSource = New DataView(d) With {.RowFilter = "[WhichList] = 2"}
    ListBox2.DisplayMember = "ShowText"
    ListBox3.DataSource = New DataView(d) With {.RowFilter = "[WhichList] = 3"}
    ListBox3.DisplayMember = "ShowText"
    ListBox4.DataSource = New DataView(d) With {.RowFilter = "[WhichList] = 4"}
    ListBox4.DisplayMember = "ShowText"
End Sub



Private Sub ListBoxX_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick, ListBox2.DoubleClick, ListBox3.DoubleClick
    DirectCast(DirectCast(sender, ListBox).SelectedItem, DataRowView).Row("WhichList") = 4
End Sub
运行程序并双击各个列表框中的各个项目


我喜欢Caius Jard的方法。对于两个表单,您必须使该数据表对两个表单都可访问。然后,您将使用“WhichList”列跟踪数据是否应显示在frm1或frm2中。您还可以使用另一列来跟踪每个项目所属的列表。这肯定有用

但是,这里介绍了如何使用类似的方法将所有选定的项目移动到您已有的项目:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' MOVE all selected items from lst1, lst2 and lst3 in frm1 to the same named controls in frm2:
    Dim ListBoxes() As ListBox = {lst1, lst2, lst3}
    For Each lb As ListBox In ListBoxes
        ' Get a reference to the control in frm2:
        Dim lbOther As ListBox = frm2.Controls.Find(lb.Name, True).FirstOrDefault
        If Not IsNothing(lbOther) Then
            ' Make a list from all the selected indices in each ListBox
            Dim indices As New List(Of Integer)(lb.SelectedIndices.Cast(Of Integer))
            ' Add those items to frm2
            For Each i As Integer In indices
                lbOther.Items.Add(lb.Items(i))
            Next
            ' Reverse the list so we can delete the indices without messing things up:
            indices.Reverse()
            For Each i As Integer In indices
                lb.Items.RemoveAt(i)
            Next
        End If
    Next
End Sub

你可以为…建立一个。。。在循环表单的旁边,每个循环都在不同的表单上运行相同的过程。将所有这些列表框绑定到相同的集合不是一个更简单的解决方案吗?您可能想看看.net LinkedList(of t)类。你有什么错误吗?什么不起作用?