【发布时间】:2018-02-03 22:29:27
【问题描述】:
我的主表单显示了一些信息,并且可以在第二个表单中编辑相同的信息。主页中的信息从数据库加载(到 DGV),并在触发 TabControl 侦听器时加载。在第二种形式中,我有一个更改数据库中该信息的按钮,当我更改它时,我的主窗体显示错误信息,直到我自己实际触发该 TabControl 侦听器。当我点击第二种形式的按钮时,我应该如何让 TabControl 监听器自动调用?
【问题讨论】:
我的主表单显示了一些信息,并且可以在第二个表单中编辑相同的信息。主页中的信息从数据库加载(到 DGV),并在触发 TabControl 侦听器时加载。在第二种形式中,我有一个更改数据库中该信息的按钮,当我更改它时,我的主窗体显示错误信息,直到我自己实际触发该 TabControl 侦听器。当我点击第二种形式的按钮时,我应该如何让 TabControl 监听器自动调用?
【问题讨论】:
如果第二个表单是一个弹出对话框,将完成然后关闭,你可以这样做:
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;
}
【讨论】: