Solved

moving items from one listbox to several others

Posted on 2014-02-11
5
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 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 44

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 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