Link to home
Start Free TrialLog in
Avatar of CASorter
CASorterFlag for United States of America

asked on

moving items from one listbox to several others

i have the below code that moves selected items from a MainListBox to a different listbox with a left click
and moves the selected items in the 2nd list box back with a right click.


    Private Sub ListBox1_Mousedown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
        Dim row_String As String
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If ListBox1.SelectedIndex >= 0 Then
                MainListBox.Items.Add(ListBox1.SelectedItem.ToString)
                ListBox1.Items.Remove(ListBox1.SelectedItem)
            End If
        Else
            Do While (MainListBox.SelectedItems.Count > 0)
                row_String = MainListBox.SelectedItem.ToString
                ListBox1.Items.Add(row_String)
                MainListBox.Items.Remove(MainListBox.SelectedItem)
            Loop
        End If
    End Sub




i want to have 20+ list boxes that can be fed from the mainlistbox and each go back to the mainlistbox

the idea is to
select 3 items from main, put in listbox1  
select 5 items from main, put in listbox3
select 1 item from listbox1, put back in main
select 7 items from main, put in listbox6
select 1 item from lisbox3, put back in main
select 2 items from main, put in listbox2  


i could brute force it and put the same code above for each listboxX ...  but is there a more elegant way?
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CASorter

ASKER

oh...  close...

but dont i still need separate mousedown events for each listbox?
Private Sub MoveStrings(lstSource As ListBox, lstTarget As ListBox)
        lstTarget.Items.Add(lstSource.SelectedItem.ToString)
        lstSource.Items.Remove(lstSource.SelectedItem)
    End Sub

    Private Sub ListBox1_Mousedown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown

        If e.Button = Windows.Forms.MouseButtons.Right Then
            If ListBox1.SelectedIndex >= 0 Then
                'MainListBox.Items.Add(ListBox1.SelectedItem.ToString)
                'ListBox1.Items.Remove(ListBox1.SelectedItem)
                MoveStrings(ListBox1, MainListBox)
            End If
        Else
            Do While (MainListBox.SelectedItems.Count > 0)
                'ListBox1.Items.Add(MainListBox.SelectedItem.ToString)
                'MainListBox.Items.Remove(MainListBox.SelectedItem)
                MoveStrings(MainListBox, ListBox1)
            Loop
        End If
    End Sub

    Private Sub ListBox2_Mousedown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListBox2.MouseDown

        If e.Button = Windows.Forms.MouseButtons.Right Then
            If ListBox2.SelectedIndex >= 0 Then
                MoveStrings(ListBox2, MainListBox)
            End If
        Else
            Do While (MainListBox.SelectedItems.Count > 0)
                MoveStrings(MainListBox, ListBox2)
            Loop
        End If
    End Sub

    Private Sub listbox3_Mousedown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListBox3.MouseDown

        If e.Button = Windows.Forms.MouseButtons.Right Then
            If ListBox3.SelectedIndex >= 0 Then
                MoveStrings(ListBox3, MainListBox)
            End If
        Else
            Do While (MainListBox.SelectedItems.Count > 0)
                MoveStrings(MainListBox, ListBox3)
            Loop
        End If
    End Sub
Yes (unless you trap mouse events in the form and use a case statement).
Just calling one function however does reduce the code tremendously.

          Do While (MainListBox.SelectedItems.Count > 0)
                MoveStrings(MainListBox, ListBox2)
            Loop

and
          Do While (MainListBox.SelectedItems.Count > 0)
                MoveStrings(MainListBox, ListBox3)
            Loop
...

you can still move the do loop into the function to cut down duplicate code.
code is reduced....  and still straight forward.

thanks!