How to refer to a parent item of a control used with embedwithin

I have a web application in which I am using embedwithin to put a series of containers on a page. When a checkbox is clicked in one of these embedded containers, I need to access an object contained within the parent control of the embedded container. This should work theoretically, but the problem is that the program won’t compile because until the program is run, the parent object of the embedded object doesn’t exist.

I have looked for a way to tell the compiler that the objects will exist at runtime, or that it should ignore this error, but I haven’t found anything.

How can I get this to work?

I would recommend creating an event on the Container that fires when you need to do something on the Parent. Pass whatever data you need in the Event parameters.

Then, just before you do the EmbedWithin, use an AddHandler to connect the Container Event to a method on the parent window.

Using AddHandler has the advantage that neither the parent nor child container need to know much about each other at runtime. An Event is fired and either the parent handles it or not.

So I tried using Addhandler, but I need to pass some extra parameters to the handling routine. The Xojo documentation doesn’t seem to cover this, and nothing I’ve tried so far has worked. I can’t find a good example online of passing extra parameters with Addhandler. Can you possibly give an example of how to do that or point out a link that gives an example?

Add those extra parameters to your event definition and pass them when you raise the event. The method that you supply to AddHandler has to have the same number and type of parameters as the event definition, plus one additional parameter that matches the type of object that raised the event. So if your event is

MyEvent(i as integer, s as string)

Your method signature should be

TheMethodToHandleTheEvent(c as MySnazzyContainerControl, i as integer, s as string)

AddHandler takes the method name only.

AddHandler MySnazzyInstance.MyEvent, AddressOf TheMethodToHandleTheEvent

I usually subclass ContainerControls so that they keep a list of all their embedded containers.

For i as integer = 0 to me.parent.containers.ubound if me.parent.containers(i) isA whateverClassYouWant then whateverClassYouWant(me.parent.containers(i)).someEvent end Next

I’ve done that before, but then your child has to know about the inner workings of the parent which isn’t very OO. Don’t get me wrong, it works, but it’s not as portable as using events. I did this way for years.

Using Events and AddHandler keeps the parent a black box that can respond to the event, or not. I’ve found this approach to be a bit easier to move between projects.

Two approaches that accomplish the same thing. To each their own.

Use webpage or module properties to pass extra parameters.

I appreciate all the help. I’m coming from a FoxPro background, and Xojo is definitely a new paradigm.