Well, as long as we're offering a 'buffet' of all types of solutions on this thread ... I might as well serve one up with a different flavor, a 'static' flavor, but I hope still slightly tasty :)
Another way to approach this information transfer across Classes/Forms is by use of a 'static' class that encapsulates information transfer. It might look like this:
public static class ListBoxItemManager
{
public static ListBox ListBoxOnForm1;
public static ListBox ListBoxOnForm2;
public static void CopyListForm1ToForm2()
{
ListBoxOnForm2.Items.Clear();
foreach (var lbItem in ListBoxOnForm1.Items)
{
ListBoxOnForm2.Items.Add(lbItem);
}
}
}
So, how do the references ... in the static class ... to the actual ListBoxes on the actual instances of Form1 and Form2 get assigned ?
In Form1 like this:
private Form2 f2;
private void Form1_Load(object sender, EventArgs e)
{
ListBoxItemManager.ListBoxOnForm1 = listBox1;
f2 = new Form2();
f2.Show();
}
And, in Form2's Load Event:
private void Form2_Load(object sender, EventArgs e)
{
ListBoxItemManager.ListBoxOnForm2 = listBox1;
}
So how does the instance of Form1 trigger the copying of its ListBox's items to the ListBox on Form2: assume a Button on Form1, named 'button1' :
private void button1_Click(object sender, EventArgs e)
{
ListBoxItemManager.CopyListForm1ToForm2();
}
Discussion:
1. obviously this highly specific example could be made much more general: what is now the 'CopyListForm1ToForm2' method in the static class could take one ListBox as a parameter, and be made a two-way copying facility. Or, with multiple ListBoxes on multiple Forms, the 'CopyListForm1ToForm2' method in the static class could take two parameters: a source ListBox, and a destination ListBox, etc.
... begin edit #1 ... in response to a friendly suggestion that I demonstrate how easy it is to extend this type of solution ...
public static void CopyList(ListBox sourceListBox)
{
ListBox targetListBox = sourceListBox == ListBoxOnForm1 ? ListBoxOnForm2 : ListBoxOnForm1;
targetListBox.Items.Clear();
foreach (var lbItem in sourceListBox.Items)
{
targetListBox.Items.Add(lbItem);
}
}
So here the ListBoxItemManager static class has a method added that takes a ListBox as a parameter, and 'does the right thing' to copy from the parameter specified ListBox to the 'other' ListBox.
It could be called from Form1 like this
to copy the ListBox Items from Form2's ListBox to Form1's ListBox:
private void button1_Click(object sender, EventArgs e)
{
ListBoxItemManager.CopyList(ListBoxItemManager.ListBoxOnForm2);
}
... end edit #1 ... of course this code could be refactored, simplified !
2. in fact, I would only use this type of technique where there would probably be much more complex interaction required between forms, and when there would be interaction required between, perhaps, multiple instances of different types of Forms. Or, in scenarios where multiple events being raised from multiple Forms might need to be "dipatched."
One-size does not fit all: make sure you try walking more than a few steps in any pair of new shoes :)