HELP! How I can exchange data between two forms

  • Thread starter Thread starter Macias
  • Start date Start date
M

Macias

Hi,
Please tell me how I can exchange data between two forms. My main form
is Form1, and I display a new form something like this:
private void Settings_Click(object sender, EventArgs e)
{
using (Form Setings_form = new settings())
{
Setings_form.ShowDialog();
}
}
I trying use a ref keyword, but in this solution I only can exchange
data between Form1 and Settings form constructor only.

Please Help.

Maciek
 
First, you'll need an instance to communicate with. If you want to
communicate with Form1 from settings, you'll have to give it that instance.

That could be in the constructor (new settings(this) in Settings_Click by
overloading the constructor (i.e. add settings(Form1 form)...).

Or, if you're calling ShowDialog, you're likely going to want Form1 to be
the owner of settings, so you can make the settings.Owner property the Form1
form by using ShowDialog like this:
Settings_form.ShowDialog(this);

You can then get an instance of Form1 from the Owner property as follows:
Form1 form1 = this.Owner as Form1;

Once you have an instance of Form1, I suggest you use properties to
communicate data from one form to another. For example, if settings allows
the user to enter a Maximum number of some sort, you would add a Maximum
property to Form1 and set it from settings like this:
form1.Maximum = maximum;
 
Back
Top