Custom events and containers

Hi all,

Grateful for any help with this problem…
Instead of using a pagepanel to navigate the multiple sections of a fairly extensive database app, i’ve decided to parcel off each section as a containerControl and have some data that glues this together in a module. The benefit is that i can parcel off code, control names etc that may conflict in to the containers and deal with them as a separate entity from the main window - the pagepanel way was becoming too unwieldy.

Works really quite well, but it’s tricker when a message needs to go from the container to the app for some other action. In order to do this, I use custom events but not entirely happy with the way I’ve implemented this:

  • in my container control class, i create the event definition
  • within the container control, i raise the event within some event of the control that will generate the message
  • i drag the containerControl into my main window and add the custom event to this with the appropriate method
    This all works just fine - however it does require me to drag all the containers into my window in the IDE and wire this all up beforehand.

The result is the main window becomes ridiculously cluttered. And it requires all containers to be initialised when the window loads, which isn’t ideal.

I know i can create a new instance of each container in code and embed it in the window. But i’m not sure how to add the custom event at runtime - very grateful for advice…

Although I haven’t built exactly what you describe (yet), would using AddHandler help?

Also see Controls on Layouts: Who owns what? – Xojo Programming Blog

if you adding controls by code then it is invisible in the designer preview,
means it is harder to add all event by yourself with addhandler or put them to the right location.

u can make properties visible in the designer if you use the menu inspector behavior.

screenshot?
do you put everything overlapped in one window?

Thanks @Scott Cadillac - but haven’t been able to understand how to use Addhandler properly (have tried… epic fails so far) - was hoping to get some concrete guidance but experts on this forum. Will check out your link so see it illuminates his more for me…

@Markus Rauch - yes, all overlapping. Navigation is done by containerControl.hide and containerControl.show. Also, if the overlap is exact, overlaying containers become ‘children’ of the underlying containers which screws it all up - so containers have to spill out of the window in the IDE so as to not exactly overlap.

As it say - this works perfectly fine in the built app, but it’s a bit messy in the IDE and requires that all containers can be initialised with (sometimes) nil values. Would really appreciate if someone could give a detailed outline of how to add the custom event to a newly created instance of the controlClass - i’m afraid documentation and searching so far haven’t made it any clearer for me…

Many thanks!

AddHandler is the correct solution to your problem. The trick is that the method you use to handle the event has to have an extra parameter that is the type of class that is raising the event. Also, AddHandler is a directive, not a function, so don’t try to use parentheses around the arguments. So if your container control class has an event KeyDown(key as string) as Boolean, you will create a function to handle it like

Function HandleKeyDown(cc as ContainerControl, key as string) as Boolean
    ...
End Function

And use it like

dim c as MyContainerClass
c = new MyContainerClass
AddHandler c.KeyDown, AddressOf HandleKeyDown

Does that help?

Thanks @Tim Hare - that helps make it clearer. Will see if I can implement this (also looking at using interfaces instead of custom events)

then why you do not just use one ui control for navigation and a class behind?
you only need to switch for what the navigation is used.

Typically, you use an Interface to communicate from the outside world (the window, for example) to the container, and events to communicate from the container to the outside world.