CASorter
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.Mouse EventArgs) Handles ListBox1.MouseDown
Dim row_String As String
If e.Button = Windows.Forms.MouseButtons .Right Then
If ListBox1.SelectedIndex >= 0 Then
MainListBox.Items.Add(List Box1.Selec tedItem.To String)
ListBox1.Items.Remove(List Box1.Selec tedItem)
End If
Else
Do While (MainListBox.SelectedItems .Count > 0)
row_String = MainListBox.SelectedItem.T oString
ListBox1.Items.Add(row_Str ing)
MainListBox.Items.Remove(M ainListBox .SelectedI tem)
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?
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.Mouse
Dim row_String As String
If e.Button = Windows.Forms.MouseButtons
If ListBox1.SelectedIndex >= 0 Then
MainListBox.Items.Add(List
ListBox1.Items.Remove(List
End If
Else
Do While (MainListBox.SelectedItems
row_String = MainListBox.SelectedItem.T
ListBox1.Items.Add(row_Str
MainListBox.Items.Remove(M
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Private Sub MoveStrings(lstSource As ListBox, lstTarget As ListBox)
lstTarget.Items.Add(lstSou rce.Select edItem.ToS tring)
lstSource.Items.Remove(lst Source.Sel ectedItem)
End Sub
Private Sub ListBox1_Mousedown(sender As Object, e As System.Windows.Forms.Mouse EventArgs) Handles ListBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons .Right Then
If ListBox1.SelectedIndex >= 0 Then
'MainListBox.Items.Add(Lis tBox1.Sele ctedItem.T oString)
'ListBox1.Items.Remove(Lis tBox1.Sele ctedItem)
MoveStrings(ListBox1, MainListBox)
End If
Else
Do While (MainListBox.SelectedItems .Count > 0)
'ListBox1.Items.Add(MainLi stBox.Sele ctedItem.T oString)
'MainListBox.Items.Remove( MainListBo x.Selected Item)
MoveStrings(MainListBox, ListBox1)
Loop
End If
End Sub
Private Sub ListBox2_Mousedown(sender As Object, e As System.Windows.Forms.Mouse EventArgs) 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.Mouse EventArgs) 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
lstTarget.Items.Add(lstSou
lstSource.Items.Remove(lst
End Sub
Private Sub ListBox1_Mousedown(sender As Object, e As System.Windows.Forms.Mouse
If e.Button = Windows.Forms.MouseButtons
If ListBox1.SelectedIndex >= 0 Then
'MainListBox.Items.Add(Lis
'ListBox1.Items.Remove(Lis
MoveStrings(ListBox1, MainListBox)
End If
Else
Do While (MainListBox.SelectedItems
'ListBox1.Items.Add(MainLi
'MainListBox.Items.Remove(
MoveStrings(MainListBox, ListBox1)
Loop
End If
End Sub
Private Sub ListBox2_Mousedown(sender As Object, e As System.Windows.Forms.Mouse
If e.Button = Windows.Forms.MouseButtons
If ListBox2.SelectedIndex >= 0 Then
MoveStrings(ListBox2, MainListBox)
End If
Else
Do While (MainListBox.SelectedItems
MoveStrings(MainListBox, ListBox2)
Loop
End If
End Sub
Private Sub listbox3_Mousedown(sender As Object, e As System.Windows.Forms.Mouse
If e.Button = Windows.Forms.MouseButtons
If ListBox3.SelectedIndex >= 0 Then
MoveStrings(ListBox3, MainListBox)
End If
Else
Do While (MainListBox.SelectedItems
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.
Just calling one function however does reduce the code tremendously.
Do While (MainListBox.SelectedItems
MoveStrings(MainListBox, ListBox2)
Loop
and
Do While (MainListBox.SelectedItems
MoveStrings(MainListBox, ListBox3)
Loop
...
you can still move the do loop into the function to cut down duplicate code.
ASKER
code is reduced.... and still straight forward.
thanks!
thanks!
ASKER
but dont i still need separate mousedown events for each listbox?