如何在Windows应用程序中关闭子窗口时刷新父窗口

时间:2017-09-14 07:28:05

标签: .net vb.net winforms

我有一个父窗口和一个子窗口。在保存子窗口时,我收到警告消息,然后按下确定。按下确定我的孩子关闭,父母得到更新但父母表格上的前一个对象仍然是他们的。 现在,如何关闭较旧的父页面[更新前]。

点击确定后,我只需要返回更新的父页面。

结论:但是,点击确定,我得到了较旧的父窗口和新的父窗口。

单击ok代码,如下所示:

Me.Close() 'for closing the child window.            
Dim frmparent As Form = New frmDomain 'for opening the parent window with updation.
frmDomain2.StartPosition = FormStartPosition.CenterScreen 'to open it in center.
frmDomain2.MdiParent = frmMain
frmDomain2.Show()

2 个答案:

答案 0 :(得分:1)

更新父级的方式取决于打开子窗口,模态或无模式的方式:

  • 模态:您的子窗口获得焦点并阻止父窗口。
    • 您必须使用childForm.ShowDialog()
    • 打开它
    • 之后的行将在子表单关闭之前执行。这意味着,您可以在ShowDialog()
    • 之后立即调用更新方法

这样:

Public Sub SomeMethod() 
  'Code in the parent:
  Dim childForm as New MyChildForm()
  childForm.ShowDialog()
  Me.UpdateParentMethod()
  'other stuff the parent has to do
End
  • 无模式:您的子窗口不会阻止父窗口。
    • 您使用child.Show()将孩子打开为无模式。
    • 在父级中,执行后面的行,父级继续以正常方式运行。
    • 为了检测子表单何时关闭,您必须从父表单订阅其OnFormClosed事件,并在那里执行您的refesh代码。

这样:

Public Sub SomeMethod() 
  Dim childForm as New MyChildForm()
  AddHandler childForm.FormClosed, AddressOf ChildForm_FormClosed
  childForm.Show()
  'other stuff the parent has to do
End

Public Sub ChildForm_FormClosed(sender as Object, e as FormClosedEventArgs)
  Me.UpdateParentMethod()
End

更多信息:

答案 1 :(得分:0)

如果您将窗口称为"子窗口",则必须从父窗口调用它"。

如果我正确地理解了您的问题,请在您称之为chiled的内部,在您声明孩子的函数的末尾放置一个刷新函数。即:

Dim FrmChild As Form = New ChildForm 
FrmChild .MdiParent = Me
FrmChild.Show()

call MyUpdateFormFunction()  -- << this will be executed after FrmChild will be closed