As already pointed out the confusion seems to be with referencing the instance instead of the class.
Try the following:
- In your project make sure that you have Form1 and Form2.
- Create two buttons on Form1, button1 and button2
- Add a textbox named textBox1 on Form2
- Add the following code to Form1
public partial class Form1 : Form {
private Form2 _form2Instance;
public Form1() {
InitializeComponent();
this.button1.Click += button1_Click;
this.button2.Click += button2_Click;
}
private void button1_Click(object sender, EventArgs e) {
if (this._form2Instance == null) {
this._form2Instance = new Form2();
this._form2Instance.Show();
}
this._form2Instance.MyText = "Some text from an instance of Form1";
}
private void button2_Click(object sender, EventArgs e) {
if (this._form2Instance != null) {
System.Windows.Forms.MessageBox.Show(this._form2Instance.MyText);
}
}
}
- and on Form2 use the following code
public partial class Form2 : Form {
public string MyText {
get {
return this.textBox1.Text;
}
set {
this.textBox1.Text = value;
}
}
public Form2() {
InitializeComponent();
}
}
Now try running the code, place breakpoints on the methods and investigate what happens. I hope this gives you better understanding...