本文介绍了如何使用数据源在列表框中添加或删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个名为listbox1和listbox2的列表框.
有四个按钮,< ,<<, >, >>

在listbox1中,我填充名为partymas的表中的数据,该表的列partyname和partyid.

当我从listbox1中选择多个项目,然后单击button1(<)时,所有选中的项目将从listbox1中删除,并添加到listbox2中.

请帮我怎么做

从OP的答案移至:

I have two listbox named listbox1 and listbox2
There are four buttons, < ,<<, >, >>

In listbox1 I fill up the data from table named partymas which has columns partyname and partyid.

When I select multiple items from listbox1 and then click on button1(<) then all the selected items are removed from the listbox1 and added into listbox2.

Please help me how can do this

Moved from OP''s answer:

Imports System.Data.SQLite
Imports ComponentFactory.Krypton.Toolkit
Public Class frm_listbox
    Dim sqlconnect As New SQLiteConnection("Data Source=|datadirectory|\abcpayment;" & "version = 3;")
    Dim mydataset2 As Data.DataSet
    Dim mydataset As Data.DataSet
    Public Function listone()
        Dim sqlcommand As New SQLiteCommand("select partyid,partyname from partymas", sqlconnect)
        Dim mydata_adapter As New SQLiteDataAdapter(sqlcommand)
        Dim mydataset0 As New DataSet
        mydata_adapter.Fill(mydataset0)
        lst_left.DataSource = mydataset0.Tables(0)
        lst_left.DisplayMember = "partyname"
        lst_left.ValueMember = "partyid"
        mydataset = mydataset0.Copy()
        mydataset2 = mydataset0.Clone()
        lst_right.DataSource = mydataset2.Tables(0)
        lst_right.DisplayMember = "partyname"
        lst_right.ValueMember = "partyid"
        'MessageBox.Show(mydataset2.Tables(0).Rows(0)(1).ToString)
        Return 0
    End Function
    Public Function moveright()
        Dim str(lst_left.SelectedItems.Count - 1) As String
        Dim g As Integer = 0
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        For k = 0 To lst_left.Items.Count - 1
        If (lst_left.GetSelected(k)) Then
        str(g) = lst_left.GetItemText(lst_left.Items(k))
        g = g + 1
        End If
        Next
        For j = 0 To str.Length - 1
        For i = 0 To mydataset.Tables(0).Rows.Count - 1
        If (str(j) = mydataset.Tables(0).Rows(i)(1).ToString) Then
        Dim objrow As System.Data.DataRow = mydataset.Tables(0).Rows(i)
        mydataset2.Tables(0).ImportRow(objrow)
        mydataset.Tables(0).Rows(i).Delete()
        mydataset.Tables(0).Rows(i).AcceptChanges()
        Exit For
        End If
        Next
        Next
        lst_right.DataSource = mydataset2.Tables(0)
        lst_left.DataSource = mydataset.Tables(0)

        Return 0
    End Function
    Public Function moverightall()
        Dim str(lst_left.Items.Count - 1) As String
        Dim g As Integer = 0
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        For k = 0 To lst_left.Items.Count - 1
        str(g) = lst_left.GetItemText(lst_left.Items(k))
        g = g + 1
        Next
        For j = 0 To str.Length - 1
        For i = 0 To mydataset.Tables(0).Rows.Count - 1
        If (str(j) = mydataset.Tables(0).Rows(i)(1).ToString) Then
        Dim objrow As System.Data.DataRow = mydataset.Tables(0).Rows(i)
        mydataset2.Tables(0).ImportRow(objrow)
        mydataset.Tables(0).Rows(i).Delete()
        mydataset.Tables(0).Rows(i).AcceptChanges()
        Exit For
        End If
        Next
        Next
        lst_right.DataSource = mydataset2.Tables(0)
        lst_left.DataSource = mydataset.Tables(0)
        Return 0
    End Function
    Public Function moveleft()
        Dim str(lst_right.SelectedItems.Count - 1) As String
        Dim g As Integer = 0
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        For k = 0 To lst_right.Items.Count - 1
        If (lst_right.GetSelected(k)) Then
        str(g) = lst_right.GetItemText(lst_right.Items(k))
        g = g + 1
        End If
        Next
        For j = 0 To str.Length - 1
        For i = 0 To mydataset2.Tables(0).Rows.Count - 1
        If (str(j) = mydataset2.Tables(0).Rows(i)(1).ToString) Then
        Dim objrow As System.Data.DataRow = mydataset2.Tables(0).Rows(i)
        mydataset.Tables(0).ImportRow(objrow)
        mydataset2.Tables(0).Rows(i).Delete()
        mydataset2.Tables(0).Rows(i).AcceptChanges()
        Exit For
        End If
        Next
        Next
        lst_right.DataSource = mydataset2.Tables(0)
        lst_left.DataSource = mydataset.Tables(0)
        Return 0
    End Function
    Public Function moveleftall()
        Dim str(lst_right.Items.Count - 1) As String
        Dim g As Integer = 0
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        For k = 0 To lst_right.Items.Count - 1
        str(g) = lst_right.GetItemText(lst_right.Items(k))
        g = g + 1
        Next
        For j = 0 To str.Length - 1
        For i = 0 To mydataset2.Tables(0).Rows.Count - 1
        If (str(j) = mydataset2.Tables(0).Rows(i)(1).ToString) Then
        Dim objrow As System.Data.DataRow = mydataset2.Tables(0).Rows(i)
        mydataset.Tables(0).ImportRow(objrow)
        mydataset2.Tables(0).Rows(i).Delete()
        mydataset2.Tables(0).Rows(i).AcceptChanges()
        Exit For
        End If
        Next
        Next
        lst_right.DataSource = mydataset2.Tables(0)
        lst_left.DataSource = mydataset.Tables(0)
        Return 0
    End Function
    Private Sub frm_listbox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        listone()
        btn_allshiftleft.Enabled = False
        btn_shiftleft.Enabled = False
        btn_shiftright.Enabled = False
        lst_left.SelectedIndex = -1
    End Sub
    Private Sub btn_exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_exit.Click
        Me.Close()
    End Sub
    Private Sub btn_shiftright_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_shiftright.Click
        moveright()
        lst_right.SelectedIndex = -1
        lst_left.SelectedIndex = -1
    End Sub
    Private Sub btn_allshiftright_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_allshiftright.Click
        moverightall()
        lst_right.SelectedIndex = -1
    End Sub
    Private Sub btn_shiftleft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_shiftleft.Click
        moveleft()
        lst_left.SelectedIndex = -1
        lst_right.SelectedIndex = -1
        End Sub
        Private Sub btn_allshiftleft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_allshiftleft.Click
        moveleftall()
        lst_left.SelectedIndex = -1
        'btn_allshiftright.Enabled = True
        'btn_allshiftleft.Enabled = False
    End Sub
    'Private Sub lst_right_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lst_right.KeyDown
    ' MsgBox(lst_right.SelectedIndex)
    'End Sub
    Private Sub lst_left_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lst_left.MouseClick
        MsgBox(lst_left.SelectedIndex)
    End Sub
    Private Sub lst_left_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lst_left.SelectedIndexChanged
        If lst_left.SelectedItems.Count >= 0 Then
        btn_shiftright.Enabled = True
        btn_allshiftleft.Enabled = False
        btn_allshiftright.Enabled = False
        btn_shiftleft.Enabled = False
        End If
        If lst_left.SelectedItems.Count = lst_left.Items.Count Or lst_left.SelectedItems.Count = lst_left.Items.Count - lst_right.Items.Count Or lst_left.SelectedIndex = -1 Then
        btn_allshiftright.Enabled = True
        btn_allshiftleft.Enabled = False
        btn_shiftleft.Enabled = False
        btn_shiftright.Enabled = False
        End If
    End Sub
    Private Sub lst_right_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lst_right.SelectedIndexChanged
        If lst_right.SelectedItems.Count >= 0 Then
        btn_shiftleft.Enabled = True
        btn_allshiftleft.Enabled = False
        btn_allshiftright.Enabled = False
        btn_shiftright.Enabled = False
        End If
        If lst_right.SelectedItems.Count = lst_right.Items.Count Or lst_right.SelectedItems.Count = lst_right.Items.Count - lst_left.Items.Count Or lst_right.SelectedIndex = -1 Then
        btn_allshiftleft.Enabled = True
        btn_allshiftright.Enabled = False
        btn_shiftleft.Enabled = False
        btn_shiftright.Enabled = False
        End If
    End Sub
End Class

推荐答案


这篇关于如何使用数据源在列表框中添加或删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 06:35