首页 > 解决方案 > 将项目从 listbox1 移动到 listbox2 并返回 vbnet winform

问题描述

我需要将所选项目从 listbox1 移动到 listbox2 并返回。
为此,我首先从服务器检索数据并用两列(名称和 ID)填充数据表。
接下来,我将数据表绑定到 listbox1,以便将 name 作为显示名称,将 id 作为值。
现在我只想将所选项目从 listbox1 移动到 listbox2(我们称之为篮子)。
这样做时,我希望所有移入 listbox2 的选定项目从 listbox1 中消失。
如果出现错误,我希望能够将所选项目从 listbox2 移回 listbox1。
这样做时,我希望 listbox2 中的选定项目消失并在 listbox1 中再次可用。
完成后,listbox2 中的所有项目都将使用它们的名称和 ID 进行处理。
我希望一切都清楚,并提前感谢您的任何建议或帮助。

标签: vb.netlistbox

解决方案


对我来说,它看起来像这样

截屏

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox2.Items.Add(ListBox1.SelectedItem)
        ListBox1.Items.Remove(ListBox1.SelectedItem)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ListBox1.Items.Add(ListBox2.SelectedItem)
        ListBox2.Items.Remove(ListBox2.SelectedItem)
    End Sub
End Class

在这段代码中,我使用了 2 个列表框,ListBox1 和 ListBox2,并在每个列表框中随机播放所选项目,我编写了第一个按钮将项目从 LB1 移动到 LB2,并使用第二个按钮将所选项目从 LB2 移动到 LB1。如果要在数据库中存储相同的记录,请使用 update 方法进行更新。


推荐阅读