-
May 29th, 2013, 03:58 AM
#1
Thread Starter
New Member
Finding Child Forms Closing Point
hi everyone,
I am using a MDI form which contains many childs. I want to execute a certain code "only if the child is individually close". And I dont want that executed if the main MDI form is closed. I tried putting a boolean global variable in the MDI_Unload and MDI_Terminate events. And checking the boolean at the form_unload event of child, still the code executes.
Example:
Private Sub MDIForm_Terminate() 'In Main Form
CloseAllTabs = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not CloseAllTabs Then MsgBox Me.Caption
End Sub
the message box pops up everytime. Even if MDIForm is closed. Help me.
thanks in advance.
-
May 29th, 2013, 07:25 AM
#2
Re: Finding Child Forms Closing Point
Last edited by SamOscarBrown; May 30th, 2013 at 12:42 PM.
Reason: Deleting
-
May 29th, 2013, 07:28 AM
#3
Re: Finding Child Forms Closing Point
Assuming CloseAllTabs boolean variable is defined in the module...try the following.
Instead of MDIForm_Terminate use MDIForm_QueryUnload event handler:
Code:
'mdi form
Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
CloseAllTabs = True
End Sub
'child form
Private Sub Form_Load()
CloseAllTabs = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not CloseAllTabs Then
Debug.Print "Closing MDI Child form..."
Else
Debug.Print "Closing MDI form..."
End If
End Sub
To see in what sequence events are triggered place Debug.Print <event name> in various events, then run and close the form.
-
May 29th, 2013, 07:35 AM
#4
Re: Finding Child Forms Closing Point
If you do that then you should get these results:
Code:
Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Debug.Print "MDIForm_QueryUnload"
End Sub
Private Sub MDIForm_Unload(Cancel As Integer)
Debug.Print "MDIForm_Unload"
End Sub
Private Sub MDIForm_Terminate()
Debug.Print "MDIForm_Terminate"
End Sub
'results:
MDIForm_QueryUnload
MDIForm_Unload
MDIForm_Terminate
As you can see Terminate event is triggered last and after all child forms are closed - this is basically why your CloseAllTabs flag is still False.
-
May 29th, 2013, 07:38 AM
#5
Re: Finding Child Forms Closing Point
Use the event QueryUnload where you can know what cause the form close, e.g.
Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormMDIForm Then
MsgBox "My MDI parent is closing"
ElseIf UnloadMode = vbFormControlMenu Then
MsgBox "Just me is closing"
' Add extra check for other UnloadMode (if you need)
End If
End Sub
-
May 29th, 2013, 08:20 AM
#6
Re: Finding Child Forms Closing Point
@4x2y:
did you even read post #3 before posting?
-
May 29th, 2013, 08:31 AM
#7
Re: Finding Child Forms Closing Point
Originally Posted by RhinoBull
@4x2y:
did you even read post #3 before posting?
Sorry, i didn't intended to repeat your solution because i saw it after posting, unfortunately this is frequently happens.
Edit:
After reading your previous posts thoroughly, i detect that i didn't repeat because you are using the MDI's QueryUnload event while i'm using the child form's QueryUnload so there isn't any reason for a global variable.
Last edited by 4x2y; May 29th, 2013 at 08:42 AM.
-
May 30th, 2013, 01:31 AM
#8
Thread Starter
New Member
Re: Finding Child Forms Closing Point
thank you so much Rhinobull and 4x2y, it helped
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|