First of all you have to create an instance of Form2.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Form2 form2;
public Form1()
{
InitializeComponent();
form2 = new Form2();
}
private void button1_Click(object sender, EventArgs e)
{
String value1 = File.ReadAllText(textBox1.Text);
foreach (string line in value1.Split('\n'))
{
form2.listBox1.Items.Add(line);
}
}
}
}
Your specific error is because of listBox1 is being private. Change it to public and you can access it.
public System.Windows.Forms.ListBox listBox1;
Also since you already have:
using System.Windows.Forms;
You can write
public ListBox listBox1;
2
solved Accessing Forms data from another form