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();

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

public partial class ScndForm : Form
   public ScndForm()
   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?

7 Years
Discussion Span
Last Post by ashishkumar008

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.


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.