Use static classes instead for data shared between forms. Let the above code be the same. Add a new class using "Project->Add Class..". Then make the class static by adding
static
before
class
. Then add the FileName variable which should be both public and static.
Let the class name be "Shared". The class should look like the following:
public static class Shared
{
public static string FileName;
}
Then in the constructor of Form2 change
this.textBox1.Text = Filename1;
to
this.textBox1.Text = Shared.FileName;
And then in the method "button1_Click" change:
Filename1 = openFileDialog1.FileName;
to
Shared.FileName = openFileDialog1.FileName;
Delete these lines as they are no longer needed:
private string Filename;
public string Filename1
{
get { return Filename; }
set { Filename = value; }
}