Howto: Catching an window close event?

F

fred

Hi, without modifying the code in "Form2" to specifically raises an event,
is there a way of determining when either of the two instances of "Form2"
below, called MDIWindow1 and MDIWindow2, closes?

Reasons: I have an MDI app with multiple windows opened. I want to disable a
"Print" button in the toolbar the second the last MDI window is closed (so
need to catch an event when the MDI window is closed, see if it is the last
one, and if so disable the "Print" button).

Thank you
Frederic Marè


Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
'blah
#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles Button1.Click
Dim MDIWindow1 As New Form2
Dim MDIWindow2 As New Form2
MDIWindow1.MdiParent = Me
MDIWindow2.MdiParent = Me
MDIWindow1.Show()
MDIWindow2.Show()
End Sub
End Class
 
H

hexathioorthooxalate

Fred, use AddHandler to implement a sub to catch the event. Your routine is
modified below:
Regards
Hexathioorthooxalate



Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim MDIWindow1 As New Form2
Dim MDIWindow2 As New Form2
MDIWindow1.MdiParent = Me
MDIWindow2.MdiParent = Me

AddHandler MDIWindow1.Closing, AddressOf OnMDIWindowClosing
AddHandler MDIWindow2.Closing, AddressOf OnMDIWindowClosing

MDIWindow1.Show()
MDIWindow2.Show()
End Sub

Private Sub OnMDIWindowClosing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs)
'put your stuff in here for toolbar disabling using
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top