我想使用 vb.net 创建 2 个 windows 窗体
第一个窗体是主窗体,第二个窗体是子窗体,它们在运行程序时同时出现
当我关闭子窗口表单时,它不会关闭我的程序,如果我关闭主窗口表单,它将关闭我的程序。
我怎样才能做到这一点 ?
我想使用 vb.net 创建 2 个 windows 窗体
第一个窗体是主窗体,第二个窗体是子窗体,它们在运行程序时同时出现
当我关闭子窗口表单时,它不会关闭我的程序,如果我关闭主窗口表单,它将关闭我的程序。
我怎样才能做到这一点 ?
当您打开第二个表单时,将所有者设置为父表单,当您关闭父表单时,它也会强制子表单关闭。我相信(至少在 VB 历史上似乎曾经这样做过)如果你隐藏父级,它也会隐藏子级。
如果您SHOW
用于显示“子表单”,请将父表单传递给它,如下所示:
Form2.show(Form1)
If you are trying to close the owner form when the child form is closed and (if the owner is the starting form of the entire program) end the program, then you will first want to do what Steve has answered to assign form1 as the owner of form2.
Then, to make the child form close the parent, use the following code in form2's FormClosed
event:
Private Sub Form2_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
Me.Owner.Dispose()
End Sub