Here is what the parent form looks like
using System;
using System.Windows.Forms;
using System.Collections.Generic;
namespace WindowsFormsApplication1
{
public partial class ParentForm : Form
{
private Button button1;
private ListBox listBox1;
private ChildForm childForm = new ChildForm();
public ParentForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(94, 219);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.listBox1.FormattingEnabled = true;
this.listBox1.Items.AddRange(new object[] {
"item1",
"item2",
"item3",
"item4",
"item5"});
this.listBox1.Location = new System.Drawing.Point(0, 0);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(279, 199);
this.listBox1.TabIndex = 1;
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button1);
this.Name = "ParentForm";
this.ResumeLayout(false);
}
List<String> listboxValues = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
listboxValues.Clear();
for (int i = 0; i < listBox1.Items.Count; i++)
{
listboxValues.Add((string)listBox1.Items[i]);
}
childForm.AddToCollection(listboxValues);
childForm.ShowDialog();
}
}
}
and here is what the child form would look like
using System;
using System.Windows.Forms;
using System.Collections.Generic;
namespace WindowsFormsApplication1
{
public partial class ChildForm : Form
{
private ListBox listBox1;
public ChildForm()
{
InitializeComponent();
}
internal void AddToCollection(List<string> values)
{
for (int i = 0; i < values.Count; i++)
{
listBox1.Items.Add(values[i]);
}
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
listBox1.Items.Clear();
}
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(0, 0);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(280, 186);
this.listBox1.TabIndex = 0;
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.listBox1);
this.Name = "ChildForm";
this.ResumeLayout(false);
}
}
}