将项目从一个列表框移动到另一个列表框 [英] Moving item from one listbox to another listbox

查看:97
本文介绍了将项目从一个列表框移动到另一个列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有人名和ID的列表框.
通过以下代码添加两个对象的方式:
list2.Items.Add(New MyList(.Fields("First").Value&"& .Fields("Last").Value,.Fields("ID").Value))
MyList是一个类,名称为字符串,而Itemdata则为长. (我在某个地方找到了这个)
如何将两个对象(如果这是个好词)从一个列表中放到另一个列表中?
I have list boxes with a person name and their ID .
The way I have added both objects is by the following code:
list2.Items.Add(New MyList(.Fields("First").Value & " " & .Fields("Last").Value, .Fields("ID").Value))
MyList is a class with Name as string and Itemdata as long. (I found this somewhere)
How do I drop both objects (if that is the good word) from one list into another?

推荐答案

Hi

不确定您想要什么,但是这里有一些代码可以尝试一下是否有帮助.

Not exactly sure of what you want, but here is some code to try out to see if it helps any.

' Form1 with ListBox1, ListBox2 and Button1
Option Strict On
Option Infer Off
Option Explicit On
Public Class Form1
    Dim people As New List(Of Person)
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' dummy data
        Dim n1 As New Person With {.Name = "Freddy Watts", .ID = "1234"}
        Dim n2 As New Person With {.Name = "Susan Watts", .ID = "4321"}
        Dim n3 As New Person With {.Name = "Archibald MacPherson Watts", .ID = "1234"}
        Dim n4 As New Person With {.Name = "Belinda Fancy-Pants", .ID = "4321"}
        people.AddRange({n1, n2, n3, n4})

        For Each p As Person In people
            ListBox1.Items.Add(p.Name & "  " & p.ID)
        Next

    End Sub
    ' could easily be adapted to move back the other way too
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim s As String = ListBox1.SelectedItem.ToString
        ListBox2.Items.Add(s)
        ListBox1.Items.Remove(s)
    End Sub
    Class Person
        Property Name As String
        Property ID As String
    End Class
End Class


这篇关于将项目从一个列表框移动到另一个列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆