从另一个表单调用监听器

时间:2017-08-25 20:08:37

标签: c# listener

我的主要表单显示了一些信息,并且可以以第二种形式编辑相同的信息。主页中的信息从数据库加载(到DGV),并在触发TabControl侦听器时加载。在第二种形式中,我有一个按钮,可以在数据库中更改该信息,当我更改它时,我的主窗体显示错误信息,直到我自己实际触发TabControl监听器。当我单击第二种形式的按钮时,如何使TabControl侦听器自动调用?

1 个答案:

答案 0 :(得分:0)

如果第二个表单是一个将完成然后关闭的弹出对话框,您可以这样做:

SecondForm secondForm = new SecondForm();
secondForm.ShowDialog();

// ShowDialog will block until the new form is closed.

RefreshData(); // we know that the user is done with the second form, so we can check for changes here.

否则,您可以传入对父表单的引用,并根据需要从子表单更新它。在您的父表单中:

SecondForm secondForm = new SecondForm(this); // pass a reference to the parent form into the child form's constructor
secondForm.Show(); // unlike ShowDialog(), Show() will not block the parent form.  The user can use both forms at the same time.

然后以您的孩子形式:

FirstForm ParentForm { get; set; }

public SecondForm(FirstForm parent)
{
    InitializeComponent();
    this.ParentForm = parent; // store the reference for later use
}

private void button1_Click(object sender, EventArgs e)
{
    ParentForm.Name = txtName.Text; // set a public property on the parent form
    ParentForm.Address = txtAddress.Text;

}