środa, 28 października 2009

cd... from listbox to listbox w ASP. NET (VB) czyli jak ... z jednej kontrolki do drugiej, tam i z powrotem

Kod VB:
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub btnToRight_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnToRight.Click
        Dim itemsToRemove As New List(Of ListItem)

        'loop through the selected items of the first listbox and add them to the second listbox
        For Each item As ListItem In ListBox1.Items
            If item.Selected And Not ListBox2.Items.Contains(item) Then
                Dim newItem As New ListItem(item.Text, item.Value)
                ListBox2.Items.Add(newItem)
                itemsToRemove.Add(item)
            End If
        Next

        'loop through removed items collection and remove the items from first listbox
        For Each item As ListItem In itemsToRemove
            ListBox1.Items.Remove(item)
        Next
    End Sub

    Protected Sub btnToLeft_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnToLeft.Click
        Dim itemsToRemove As New List(Of ListItem)
        'loop through the selected items of the second listbox and add them to the first listbox
        For Each item As ListItem In ListBox2.Items
            If item.Selected And Not ListBox1.Items.Contains(item) Then
                Dim newItem As New ListItem(item.Text, item.Value)
                ListBox1.Items.Add(newItem)
                itemsToRemove.Add(item)
            End If
        Next

        'loop through removed items collection and remove the items from first listbox
        For Each item As ListItem In itemsToRemove
            ListBox2.Items.Remove(item)
        Next
    End Sub
End Class

Brak komentarzy:

Prześlij komentarz