Intercettare l'aggiunta/rimozione di form MDIChild ad una form MDIParent é un'operazione che ho scoperto essere piuttosto macchinosa. L'area contenitore di form MDIChild non é altro che un controllo nascosto di tipo System.Windows.Forms.MdiClient. Questo controllo espone come tutti i controls container gli eventi di ControlAdded e ControlRemoved che corrispondo all'aggiunta o alla rimozione di una form Child.
Questo é il codice di una Form MDI base con gli eventi ChildFormAdded e Removed:
Public Class MDIParentForm
Inherits System.Windows.Forms.Form
Protected WithEvents mdiClient As System.Windows.Forms.MdiClient
#Region "Eventi"
Public Event ChildFormAdded As System.Windows.Forms.ControlEventHandler
Public Event ChildFormRemoved As System.Windows.Forms.ControlEventHandler
''' -----------------------------------------------------------------------------
'''
''' Solleva l'evento ChildFormAdded
'''
'''
'''
'''
'''
''' [michele] 23/02/2005 Created
'''
''' -----------------------------------------------------------------------------
Protected Overridable Overloads Sub OnChildFormAdded(ByVal e As System.Windows.Forms.ControlEventArgs)
RaiseEvent ChildFormAdded(Me, e)
End Sub
''' -----------------------------------------------------------------------------
'''
''' Solleva l'evento ChildFormRemoved
'''
'''
'''
'''
'''
''' [michele] 23/02/2005 Created
'''
''' -----------------------------------------------------------------------------
Protected Overridable Overloads Sub OnChildFormRemoved(ByVal e As System.Windows.Forms.ControlEventArgs)
RaiseEvent ChildFormRemoved(Me, e)
End Sub
#End Region
''' -----------------------------------------------------------------------------
'''
''' Crea una form MDIParent
'''
'''
'''
'''
''' [michele] 03/03/2006 Created
'''
''' -----------------------------------------------------------------------------
Public Sub New()
Me.IsMdiContainer = True
End Sub
''' -----------------------------------------------------------------------------
'''
''' Ritorna il controllo MDIClient della form corrente
'''
'''
'''
'''
'''
''' [michele] 03/03/2006 Created
'''
''' -----------------------------------------------------------------------------
Protected Overridable Function GetMDIClientControl() As System.Windows.Forms.MdiClient
For Each ctl As System.Windows.Forms.Control In Me.Controls
If TypeOf ctl Is System.Windows.Forms.MdiClient Then
Return DirectCast(ctl, System.Windows.Forms.MdiClient)
End If
Next ctl
End Function
''' -----------------------------------------------------------------------------
'''
''' Recupera il controllo MDIClient dalla control's collection
'''
'''
'''
'''
'''
''' [michele] 03/03/2006 Created
'''
''' -----------------------------------------------------------------------------
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Me.mdiClient = Me.GetMDIClientControl
MyBase.OnLoad(e)
End Sub
#Region "Riflessione degli eventi di MDIClient"
Private Sub mdiClient_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles mdiClient.ControlAdded
Me.OnChildFormAdded(e)
End Sub
Private Sub mdiClient_ControlRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles mdiClient.ControlRemoved
Me.OnChildFormRemoved(e)
End Sub
#End Region
End Class
powered by IMHO 1.3