I may confusing with the terminology, but here is what I’m trying to do:
I have a move function which will end up moving selected items from one listBox to another. There are three list boxes with two right and left arrow buttons in between each to move the first list box items to the middle, the middle back to the first, etc.
My function accepts the different button names via sender and in the switch statement, I want to choose which listBox are the selected items going to be send from and where they will be sent to. If that makes sense.
The while loop at the bottom is going to perform the actual move, depending on what is set for the “to” and “from” listBoxes.
My question is how can I in the scope of this function reference the names of the three existing listBoxes in each of the cases of the switch statement? I know initializing a new listBox like I’ve done is wrong and will just create some more listBoxes. Maybe for this case the easiest thing is to explicitly put the while loop in each of the case statements, but for the future in a more complex scenario, I still would like to know how this is done.
private void move(object sender, EventArgs e)
{
Button thisButton = sender as Button;
ListBox to = new ListBox();
ListBox from = new ListBox();
switch (thisButton.Name)
{
case "queueToProgressButton":
to.Name = "queueListBox";
from.Name = "progressListBox";
break;
case "progressToQueueButton":
to.Name = "queueListBox";
from.Name = "progressListBox";
break;
case "progressToCompletedButton":
to.Name = "queueListBox";
from.Name = "progressListBox";
break;
case "completedToProgressButton":
to.Name = "queueListBox";
from.Name = "progressListBox";
break;
}
while (from.SelectedItems.Count > 0)
{
to.Items.Add(from.SelectedItem);
from.Items.Remove(from.SelectedItem);
}
}
You should be using references to the existing list boxes, not allocating new ones. Also, your four branches of the
switchare identical in the code you posted; I don’t think that’s what you intended. I made adjustments to the code based on what I think you meant to do in theswitch.Try something like this instead: