I want to pass some variables from the child form to the parent form.
So at the Main form or Parent form I added the variables as a constructor:

private SampleFunction()
{
  int a, b;
  //Here is where I want to use the x,y and z from the Second Form...
  a = this._x*this._y;
  b = this._y*this._z;
}
public MainForm(int x, int y, int z)
{    InitializeComponent();
     this._x = x;
     this._y = y;
     this._z = z;
}
private void LoadVar_Click(object sender, EventArgs e)
{
  ScndForm SecForm = new ScndForm();
  SecForm.ShowDialog();
  SecForm.Update();
}

Now my question is from the Form2 or Child form how to pass the variables?

public partial class ScndForm : Form
{
   public ScndForm()
   {
        InitializeComponent();
   }
   MainForm Parent = new MainForm( x, y, z) //This Point
   // Put them global?
   int x, y, z;
   
   public void OKButton_Click(object sender, EventArgs e)
   {
       x = this.xtxtbox.Text;
       y = this.ytxtbox.Text;
       z = this.ztxtbox.Text;
   }
}

This is just a sample, not the complete code. But where is the comment //This point is giving to me an error saying: 'A field initializer cannot reference the non-static field, method or property'. What I am doing wrong?

Recommended Answers

All 3 Replies

Probably the easiest is to read the variables from the form 2 using an event.
So, if you create an public event on form2, and then create the handler of that event for form1, every time you fire the event (in from2) the handler in form1 will have the information you need.

Regards,

use static vaiables to use this on other forms.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.