Calling the parent

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?
 
J

Jack Jackson

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.
 
D

Doug Holland

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top