2017-08-25 35 views
-2

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

回答

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; 

} 
+0

第二个建议是更好,因为我希望能够在同时使用这两种形式 – Quadrition

相关问题