move same selected item in 2 listboxes

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
First let me explain my settings at the moment.
I have 2 listboxes on a form, which contain the same items (and in the same order).

I would like to know if and how I can move a selected item in one listbox and the second listbox moves the same item aswell (so they move in sync).

I can move the items in one listbox with the following code:
VB.NET:
Private Sub UpButton_Click(sender As System.Object, e As System.EventArgs) Handles UpButton.Click
        Dim index As Integer = ListBox1.SelectedIndex
        If index > 0 Then
            Dim temp As String = ListBox1.SelectedItem
            ListBox1.Items.RemoveAt(index)
            ListBox1.Items.Insert(index - 1, temp)
            ListBox1.SelectedIndex = index - 1
        End If
    End Sub

    Private Sub DownButton_Click(sender As System.Object, e As System.EventArgs) Handles DownButton.Click
        Dim index As Integer = ListBox1.SelectedIndex
        If index <> -1 AndAlso index < ListBox1.Items.Count - 1 Then
            Dim temp As String = ListBox1.SelectedItem
            ListBox1.Items.RemoveAt(index)
            ListBox1.Items.Insert(index + 1, temp)
            ListBox1.SelectedIndex = index + 1
        End If
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim index As Integer = ListBox1.SelectedIndex
        If index <> -1 Then
            UpButton.Enabled = (index > 0)
            DownButton.Enabled = (index < ListBox1.Items.Count - 1)
        End If
    End Sub

The moving up and down is being done by buttons.

Thanks for any respons
 
Ok, I solved it already.

I just copied the code for the listbox1 and made it for the second listbox and that works.
 
Back
Top