Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

moving items from one listbox to several others

Posted on 2014-02-11
5
Medium Priority
?
240 Views
Last Modified: 2014-02-11
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?
0
Comment
Question by:CASorter
  • 3
  • 2
5 Comments
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 39851122
sub moveStrings( lbSrce as ListBox, lbTrgt as ListBox)
  your code to transfer from the source listbox to the target listbox
  BUT use lbSrce and lbTrgt, not hardcode the names as in your original code
end sub

and in the mouse down handlers
....
  moveStrings(ListBox1, MainListbox)
...

or
  moveStrings(MainListBox, ListBox3)

or whichever you want to swap between
0
 

Author Comment

by:CASorter
ID: 39851484
oh...  close...

but dont i still need separate mousedown events for each listbox?
0
 

Author Comment

by:CASorter
ID: 39851522
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
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 39851700
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.
0
 

Author Closing Comment

by:CASorter
ID: 39851713
code is reduced....  and still straight forward.

thanks!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question