在 c# 中从父进程运行子窗体进程 [英] Run a child form process from parent in c#

查看:50
本文介绍了在 c# 中从父进程运行子窗体进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从其父级运行 MDI 子级.例如,我在子表单中有 3 个文本框.我想将前两个文本框中的两个值相加,并将结果写入第三个.我想在父窗体上有一个按钮(我们称之为运行按钮)来为我做这件事.此外,我还有其他子表单可以进行其他计算,因此我希望运行按钮根据聚焦的表单进行操作.有人知道我该怎么做吗?

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.

谢谢

代码很简单我在子窗体中有三个文本框,前两个中有用户输入值,我想单击父窗体上的运行按钮,子窗体中第三个文本框的值成为前两个文本框.我在子表单中有这个方法,我可以从父表单调用它,但它不起作用

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

在父表单中我有

private void button1_Click(object sender, EventArgs e)
        {
            ChildFrom child = new ChildFrom();
            child.AddValues();
        }

推荐答案

在您的子表单中:

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

在您的父表单中,您必须在您打开的子表单的同一实例上调用 AddValues.换句话说,在父表单中:

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.

如果您不想让子表单成为成员变量,那么您可以依赖 C# 中的闭包.

If you dont wan't child form to be a member variable, then you can rely on closures in C#.

在父表单中:

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

注意从初始化部分删除 btnRun 上现有的点击处理程序.

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

这篇关于在 c# 中从父进程运行子窗体进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆