so far i have accomplished transfer a single select item from one lb1 to lb2 and vice versa. Now the problem is transfering the whole list of data from lb1 to lb2 and vice versa. if anyone could help please. using for loop will be much better.
i am using the following code:
private void add_Click(object sender, EventArgs e)
{
if (lb1.SelectedItem != null)
{
lb2.Items.Add(lb1.SelectedItem);
lb1.Items.Remove(lb1.SelectedItem);
}
else
{
MessageBox.Show("No item selected");
}
}
private void remove_Click(object sender, EventArgs e)
{
if (lb2.SelectedItem != null)
{
lb1.Items.Add(lb2.SelectedItem);
lb2.Items.Remove(lb2.SelectedItem);
}
else
{
MessageBox.Show("No item selected");
}
}
private void addall_Click(object sender, EventArgs e) //problem is here. adding all the items from lb1 to lb2
{
for (int i = 0; i < lb1.SelectedItems.Count; i++)
{
lB2.Items.Add(lb1.SelectedItems[i].ToString());
}
}
private void removeall_Click(object sender, EventArgs e) //problem is here. adding all the items from lb2 to lb1
{
}
You could do something like this:
This would loop through your list and add them all. You would do the reverse for
lb2->lb1
.Simply iterate over all Items of the list and add each element to the next list. At the end simply remove all Items.
Because you want to transfer all of the items from one list box to another not only the selected items you must loop for every item in the list box and not only the selected items.
So change your usage of
ListBox.SelectedItems
toListBox.Items
and your code should work as expected assuming you remember to remove the items as well.