There are many ways to structure interchange of data between Forms. Which one you choose may involve user-interaction (showing modally), need for validation of user input, etc.
OriginalGriff has a great series of three articles here on Form to Form interaction and data exchange: [
^]
See, also: [
^]
Here's one way:
Assuming:
1. You have two Forms
2. One if them is the Main Form
3. The other Form is created at run-time by the Main Form
In the MainForm:
private OtherForm otherForm;
private ListBox otherFormListBox;
private void MainForm_Load(object sender, EventArgs e)
{
otherForm = new OtherForm;
otherFormListBox = otherForm.OFListBox;
otherForm.Show();
}
public void TransferData()
{
}
In the OtherForm:
public ListBox OFListBox;
private ListBox otherFormListBox;
private void OtherForm_Load(object sender, EventArgs e)
{
otherForm = new OtherForm;
OFListBox = listBox1;
}
In this example, you expose the ListBox in the other Form, so the Main Form can access it.