Home > database >  How to copy the first Unselected element from a listBox to another in vb.net
How to copy the first Unselected element from a listBox to another in vb.net

Time:03-07

I need code that helps me to copy the first Unselected element from a listBox to another, I already know how to copy all unselected elements as below but I need to copy just the first Elemnet from the lisbox:

                For i As Integer = 0 To ListBox3.Items.Count - 1
                    If ListBox3.GetSelected(i) Then
                        ListBox1.Items.Add(ListBox3.Items(i))
                    Else
                        ListBox1.Items.Add(ListBox3.Items(i))
                    End If
                Next

thanks

CodePudding user response:

You can try this method. It'll copy all the unselected, but easy to modify to just copy the first found.

Private Sub CopyUnselectedButton_Click(sender As Object, e As EventArgs) Handles CopyUnselectedButton.Click

    For iterator As Integer = 0 To SourceListBox.Items.Count - 1

        Dim item As Object = SourceListBox.Items(iterator)

        If Not SourceListBox.SelectedItems.Contains(item) Then
            DestListBox.Items.Add(SourceListBox.Items(iterator))
            Exit For
        End If

    Next

End Sub
  • Related