简体   繁体   English

VB6关闭父窗体时,请在子窗体中卸载queryunload或终止事件

[英]VB6 When closing the Parent form do unload queryunload or terminate events fire in Child Forms

I've asked this question before but I must not have been clear because the answer turned out not to be correct as far as I can tell .. so here goes again. 我之前已经问过这个问题,但我一定不是很清楚,因为据我所知,答案原来是不正确的。

I have a VB6 application .. it has main menu that opens up Forms and dialogs (not MDI I don't think) 我有一个VB6应用程序..它具有打开窗体和对话框的主菜单(不是我不认为的MDI)

I want to save the position of any open forms or dialogs when the application closes so the next time I open the application the positions will be restored. 我想在应用程序关闭时保存所有打开的窗体或对话框的位置,以便下次我打开应用程序时,位置将被恢复。

I hoped that when I close down the application that ANY of queryUnload, unload, terminate would fire in the 'child' windows or dialogs and I could save their position .. but nothing seems to fire. 我希望当我关闭应用程序时,任何queryUnload,unload,terminate都会在“子”窗口或对话框中触发,并且我可以保存它们的位置..但似乎没有触发。

I've put break points on all the above events, but when I close down the application nothing gets hit. 我在上述所有事件上都设置了断点,但是当我关闭该应用程序时,什么也没有遇到。

VB6 is not my usual gig .. so I'm probably thinking too much .net .. VB6不是我平常的演出..所以我可能在想太多.net ..

Is there a way this can be done in VB6 .. 有没有办法可以在VB6中完成..

Edit: So it seems if I click the close cross in the top right corner I 'end' the application. 编辑:所以,如果我单击右上角的关闭叉号,则“结束”应用程序。 Is there a way in VB6 to edit this behavior so I could instigate a graceful close down ? VB6中是否有一种方法可以编辑此行为,以便引起正常关闭?

All 3 events fire, but the Terminate event might not be fired when you expect it. 这3个事件均会触发,但您期望时可能不会触发Terminate事件。

Create a test project consisting of 1 MDI form, 1 MDI child form, 1 normal form and add the following code: 创建一个由1个MDI表单,1个MDI子表单,1个普通表单组成的测试项目,并添加以下代码:

MDI form: MDI表格:

'MDI form : name=MDIForm1

Option Explicit

Private Sub MDIForm_Click()
  End
End Sub

Private Sub MDIForm_Load()
  Form1.Show
  Form2.Show vbModeless, Me
  WindowState = vbMaximized
End Sub

Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  MsgBox "MDI form QueryUnload event"
End Sub

Private Sub MDIForm_Terminate()
  MsgBox "MDI form Terminate event"
End Sub

Private Sub MDIForm_Unload(Cancel As Integer)
  MsgBox "MDI form Unload event"
End Sub

MDI child: MDI子:

'1 form: name=Form1  MDIChild=true

Option Explicit

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  MsgBox "MDI child QueryUnload event"
End Sub

Private Sub Form_Terminate()
  MsgBox "MDI child Terminate event"
End Sub

Private Sub Form_Unload(Cancel As Integer)
  MsgBox "MDI child Unload event"
End Sub

Normal form: 正常形式:

'1 form: name=Form2

Option Explicit

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  MsgBox "Form2 QueryUnload event"
End Sub

Private Sub Form_Terminate()
  MsgBox "Form2 Terminate event"
End Sub

Private Sub Form_Unload(Cancel As Integer)
  MsgBox "Form2 Unload event"
End Sub

You will see the QueryUnload and Unload events are fired (in this order), but the Terminate event is fired when you close down the MDI form. 您将看到QueryUnload和Unload事件被触发(按此顺序),但是当您关闭MDI表单时,将触发Terminate事件。

When you click the background of the MDI form, then End will be called and no event will be fired 当您单击MDI表单的背景时,将调用End并且不会触发任何事件

"Child" forms do get the usual events. “儿童”表格确实会收到通常的事件。 Notice that on Form_QueryUnload the UnloadMode parameter is vbFormOwner . 请注意,在Form_QueryUnloadUnloadMode参数为vbFormOwner

"Child" forms are shown with explcit owner form like this: “子”表单与明显的所有者表单一起显示,如下所示:

'--- using global references
Form2.Show vbModal, Form1
Form3.Show , Form1 '--- Form3 is modeless

'--- using instances
With New Form2
    .Show vbModal, oOwnerForm
End With
With New Form3
    .Show , oOwnerForm
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM