Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

moving items from one listbox to several others

Posted on 2014-02-11
5
Medium Priority
?
239 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 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 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

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

721 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