Calling the parent

  • Thread starter Thread starter Arne Garvander
  • Start date Start date
A

Arne Garvander

I have a user control inside another user control.
Is there a way for a Child Control to call back to the parent, raising an
event in the parent or anything such?
 
I have a user control inside another user control.
Is there a way for a Child Control to call back to the parent, raising an
event in the parent or anything such?

You don't raise an event in another object. You raise an event in
your object and any other objects that have subscribed to that event
get called. In this case the child control would have to define an
event and the parent would need to subscribe to that event.

You can access methods and properties in the parent. If the parent is
an instance of SomeClass or implements a known interface:

Dim myParent As SomeClass = TryCast(Me.Parent, SomeClass)
If myParent IsNot Nothing Then
myParent.SomeMethod()
....

Interfaces are probably the best way to deal with this, then the child
doesn't have to know the exact class of the parent, you just have to
make sure that all classes that might be the child's parent implement
the interface.
 
I agree with Jack that interfaces are certainly the best answer. I've also
found that creating a class that derives from ApplicationContext is useful as
you can expose an internal property that allows any code in your application
to access the applications main form. The custom ApplicationContext is also a
great place to store other application wide information that might be useful
throughout the application. Personally I implement the ApplicationContext as
a singleton with static properties to access the application wide details
that are cached at startup.
 
Back
Top