最好的方式来处理合格Control.Checked国家形式之间最好的、合格、形式、方式

2023-09-06 13:08:50 作者:浅笑回眸√百媚生

它,因为我已经与Windows合作了一段时间Forms应用程序。我的主要形式,并根据一定的条件对一个复选框,如果第二个表需要打开从用户请求额外的数据,我应该怎么传递(或获得)一条讯息从第二主表表格,以便我可以告诉它是否没关系选中或清除该复选框?

It's been a while since I've worked with Windows Forms applications. I have a Checkbox on the Main form and, based upon a certain condition, if the Second form needs to be opened to request additional data from the user, how should I pass (or get) back a message to the Main form from the Second form so I can tell whether or not it's okay to Check or Uncheck the Checkbox?

从我记事起,我可以使用类似路过 REF 。还是有更好的方法来做到这一点?

From what I can remember, I could use something like Pass by ref. Or is there a better way to accomplish this?

推荐答案

要做到这一点是使用一个事件的一种方式。

One way to do this would be to use an event.

在你的孩子的形式,宣布任何事件上的特定用户交互待提高,并简单地订阅此事件在您的主要形式。

In your child form, declare an event to be raised upon specific user interaction, and simply "subscribe" to this event in your main form.

当你实例化并打电话给你的孩子的形式,你会做这样的:

When you instantiate and call you child form, you'd do like this:

private void button1_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2();
    frm.MyEvent += frm_MyEvent;
    frm.ShowDialog();
    frm.MyEvent -= frm_MyEvent;
}

private void frm_MyEvent(object sender, EventArgs e)
{
    textBox1.Text = "whatever"; //just for demo purposes
}

在你的孩子的形式,你声明的情况下,提高它:

In your child form, you declare the event and raise it:

public event EventHandler MyEvent;

private void button1_Click(object sender, EventArgs e)
{
    if (MyEvent!= null)
        MyEvent(this, EventArgs.Empty);
}

希望这有助于

Hope this helps