Mdi call active child form method

ConnyD

Member
Joined
Oct 1, 2006
Messages
8
Programming Experience
1-3
Hi all,

I'm trying to call a method defined within a child form from the menu strip of the parent form when the child form is active. I've spent a few hours searching on the internet to no avail. Does anyone have idea idea of how I can achieve this?

Thanks in advance
 
If you only have one type of child forms you can cast the active child to this type
VB.NET:
CType(Me.ActiveMdiChild, ChildForm).ChildMethod()
It is also possible to check the type of the active child with TypeOf operator and cast as 'this or that' type depending on the result.

If you have multiple different child form classes and they all have their own version of this method you should define an Interface and let all child forms implement it. Here is a basic sample:
VB.NET:
Public Interface IChildForm
    Public Sub ChildMethod()
End Interface

Public Class ChildFormA
    Implements IChildForm 'press Enter here to generate Interface members!

    Public Sub ChildMethod() Implements IChildForm.ChildMethod
        MsgBox("hi from A")
    End Sub
End Class

Public Class ChildFormB
    Implements IChildForm

    Public Sub ChildMethod() Implements IChildForm.ChildMethod
        MsgBox("hi from B")
    End Sub
End Class
Then you can cast the active child to IChildForm type to access this method that all IChildForm types have:
VB.NET:
CType(Me.ActiveMdiChild, IChildForm).ChildMethod()
A third option with multiple child form types is write a base class that have the default functionality and have the different childs inherit it. Either way the ActiveMdiChild property returns an object of type Form, to access more specific type Form classes you have to cast the object to that type.
 
Back
Top