Form1 has a button that opens Form2
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ExampleFormOpener
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//
//Create the second form and set the opener to this instance.
//
Form2 mySecondForm = new Form2();
mySecondForm.Opener = this;
mySecondForm.Show();
}
public void SetText(string customText)
{
this.textBox1.Text = customText;
}
}
}
Form2 has a button that sets Form1's text field
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ExampleFormOpener
{
public partial class Form2 : Form
{
private Form1 mOpener = null;
public Form2()
{
InitializeComponent();
}
public Form1 Opener
{
set { this.mOpener = value; }
}
private void button1_Click(object sender, EventArgs e)
{
if (this.mOpener != null)
{
this.mOpener.SetText("Text set from another form!");
}//if
}
}
}