I want to run a MDI child form its parent. For example I have 3 textboxes in the child form. I want to add the two values in the first two text boxes and write the results in the third. I want to have a button on the parent form (lets call it run button) to do this for me. Also, I have other child forms that do other calculations so I want the run button behaves according to the focused form. Does any one know how I should do it?
I have written a method in each child form to do the calculations and I call this method in the run button of the parent form but this does not recognize the values of child form text boxes (ie null). It would be awesome if someone could help me.
Thanks
the code is very simple
I have three text boxes in the child form and the user input values in the first two and I want to click the run button on the parent form and the value of the third text box in the child form becomes the summation of the values of the first two text boxes. I have this method in the child form which I can call it from the parent form but it does not work
public void AddValues()
{
double a = double.Parse(textBox1.Text);
double b = double.Parse(textBox2.Text);
textBox3.Text = (a + b).ToString();
}
In the parent form I have
private void button1_Click(object sender, EventArgs e)
{
ChildFrom child = new ChildFrom();
child.AddValues();
}
In your child form:
In your parent Form you have to call
AddValueson the same instance of child form which you are opening. In other words, in parent form:The key here is to operate on the same instance of the child form. If you need it throughout make it a member field in parent form.
Edit:
If you dont wan’t child form to be a member variable, then you can rely on closures in C#.
In parent form:
Take care to remove the existing click handler on
btnRunfrom the initialization part.