Run a child form process from parent in c#

≯℡__Kan透↙ 提交于 2021-01-27 18:15:32

问题


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

回答1:


In your child form:

public void AddValues() //add error handling
{
    double a = double.Parse(textBox1.Text);
    double b = double.Parse(textBox2.Text);
    textBox3.Text = (a + b).ToString();
}

In your parent Form you have to call AddValues on the same instance of child form which you are opening. In other words, in parent form:

public partial class ParentForm : Form
{
    ChildForm _cF; //member field

    public ParentForm()
    {
        InitializeComponent();
    }

    private void OpenChildForm() //this is how you should open the form
    {                            //call this function in whichever event 
         _cf = new ChildForm();  //you are opening the child form.
         _cf.Show();
    }   

    private void button1_Click(object sender, EventArgs e)
    {
        //please remove these lines, its wrong!
        //ChildFrom child = new ChildFrom();
        //child.AddValues();

        //do this instead:
        _cf.AddValues();
    }
}

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:

private void OpenChildForm()
{
     ChildForm cf = new ChildForm();
     cf.Show();

     btnRun.Clicked -= OnRunButtonClicked(cf); //important - to avoid multiple-
     btnRun.Clicked += OnRunButtonClicked(cf); //handlers getting attached.
}  

private EventHandler OnRunButtonClicked(ChildForm cf)
{
    return (sender, e) => cf.AddValues();
}

Take care to remove the existing click handler on btnRun from the initialization part.



来源:https://stackoverflow.com/questions/13152415/run-a-child-form-process-from-parent-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!