Solved

moving items from one listbox to several others

Posted on 2014-02-11
5
223 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 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now