Question how to run a custom public procedure to an ActiveMDIChild form?

adshocker

Well-known member
Joined
Jun 30, 2007
Messages
180
Programming Experience
Beginner
hi all,

my problem is that i have an MDI Parent form. it has lots of child forms. on my MDI Form i have the toolbar buttons Add, Delete and Save.

all or most of the child forms, i created a custom public procedure Sub AddNew, Sub Delete and Sub Save.

what i'm trying to do is when i click the AddNew in the MDI Form, it will run the custom public Sub AddNew of the currently ActiveMDIForm.

i have tried...

VB.NET:
DirectCast(Me.ActiveMdiChild, Form2.AddNew()

but i'm getting an error "Unable to cast object of type 'Form1' to type 'Form2'" if the currently ActiveMDIChild is not Form2.

can someone help me with this.

thanks
 
You either have to make sure all Mdi child classes have those methods (Inherits a base class that has those methods, or Implements a common Interface) or you have to check the type of the object (TypeOf/GetType) to make sure it is a type you know has those methods.
 
You either have to make sure all Mdi child classes have those methods (Inherits a base class that has those methods, or Implements a common Interface) or you have to check the type of the object (TypeOf/GetType) to make sure it is a type you know has those methods.

hi,

thanks for the response.

all my MDI Forms have AddNew() procedures.

(Inherits a base class that has those methods, or Implements a common Interface)
i tried

VB.NET:
Public Class Form2
    Inherits Form1

    Public Sub AddNew()
        Me.BindingSource.AddNew()
    End Sub
End Class

but i get this error. "Error 1 Base class 'Form1 specified for class 'Form2' cannot be different from the base class 'System.Windows.Forms.Form' of one of its other partial types.".

not sure if i'm doing it right.

can you provide me a sample code?

thanks.
 
For generated inherited classes you have to change the Designer generated code to change the Inherits statement in VB Express, with better versions you can create an inherited form right from the Add New dialog.

Here is a basic Interface example:
VB.NET:
Interface C
    Sub Something()
End Interface

Class A
    Implements C

    Public Sub SomethingX() Implements C.Something
        MessageBox.Show("A")
    End Sub
End Class

Class B
    Implements C

    Public Sub SomethingY() Implements C.Something
        MessageBox.Show("B")
    End Sub
End Class
Here different types that implement an interface can be cast to the interface type, whereby you can call the interface member, the class implementation is called regardless of how it is named (usually it is the same, but I provoked the naming in example to prove the point). Downfall with interface is each class must implement each member and can't benefit from inherited implementation. Benefit is a class can implement multiple interfaces.
VB.NET:
Dim a As New A
Dim b As New B
CType(a, C).Something()
CType(b, C).Something()
A basic Inherits example:
VB.NET:
Class Base
    Public Overridable Sub Something()
        MessageBox.Show("Base")
    End Sub
End Class

Class A
    Inherits Base
End Class

Class B
    Inherits Base

    Public Overrides Sub Something()
        MessageBox.Show("B")
    End Sub
End Class
Here all types that inherits a base class will include all members of base class, if the member is overridable the derived class can override it to provide a different implementation.
VB.NET:
Dim a As New A
Dim b As New B
a.Something()
b.Something()
 
thanks.

i'll give it a try.
 
Here is also an example with no inheritance between the types:
VB.NET:
Class A
    Public Sub SomethingX()
        MessageBox.Show("A")
    End Sub
End Class

Class B
    Public Sub SomethingY()
        MessageBox.Show("B")
    End Sub
End Class
The solution here is to check the type of the object using GetType/TypeOf and hardcoding a solution for each possible case:
VB.NET:
Dim o As Object = New A
If o.GetType Is GetType(A) Then
    CType(o, A).SomethingX()
ElseIf TypeOf o Is B Then
    CType(o, B).SomethingY()
End If
It doesn't matter if the member called has the same name here, you still have to provide a special case for the type.
 
Back
Top