"vvenk" <vv***@discussions.microsoft.com> wrote
I have two forms, FormA and FormB. FormA opens FormB. FormB needs to execute
a public method on FormA.
<...> Sub doSomething()
MyParentForm.ExecuteFunction
End Sub
So, in other words, how can I generalize it so the parameter can be any of
the form classes that will have a public method ExecuteFunction?
There are two general purpose ways to handle that, using events, or using interfaces.
Using events means that any form that wants to, can declare FormB WithEvents
such that FormB will raise the event for the parent form to act on. The parent
would not be required to listen for events, even while formB raises them as scheduled.
If the decision to act on FormB's event is supposed to be in the parent form, that
would be the way to go. If FormB requires that there be a routine to call in the
parent form, then use an interface.
Using interfaces means you declare an interface with the required method, and force
all callers to implement that interface to show FormB:
Private MyParentForm as IParentInterface
Public Sub New(psForm as IParentInterface)
...
...
MyParentForm = psForm
End Sub
Sub doSomething()
MyParentForm.ExecuteFunction
End Sub
That would assume you've declared an interface somewhere
(in a module perhaps):
Public Interface IParentInterface
Sub ExecuteFunction()
End Interface
And any form that uses FormB must implement that interface:
Public Class FormA
Inherits System.Windows.Forms.Form
Implements IParentInterface
'... <rest of form code>
Public Sub ExecuteFunction() Implements IParentInterface.ExecuteFunction
' FormB callback function goes here
End Sub
End Class
The calling syntax would be similar to what you are probably already
using:
Dim dialog As FormB = New FormB(Me)
dialog.Show()
HTH
LFS