Get container control window

Hello,
maybe i am missing something… but how can i get the window on which a container control was placed?
i want to execute a function (this function is e.g. on window1) from a container control without hardcoding the window name

.parent and truewindow does not do the job…

Thanks

btw. the container control was dragged into the window, not using embedWithin

See http://documentation.xojo.com/api/deprecated/window.html#window-truewindow .

1 Like

TrueWindow should do the job.
From the docs:

TrueWindow walks up the window hierarchy and finds the actual enclosing window regardless of how deeply nested the RectControl or ContainerControl hierarchy is.

however, you get a Window instance that you should probably cast to your Window subclass to call a method which is only available on your subclass.

how is that done, for example on window1 i have a function “listEquations()” and i want to call that from the container control without writing window1.listEquations()

i know, beginners questions :wink:

Well, if you are going to to call a window method from the container control instance, it’s probably enough to do:

self.listEquations()

thanks, but what i have is a container control (on a slider) with:

dragvalue=currentPos*value/(bar.width-pointer.width) window1.MoviePlayer1.Position=dragvalue

but “window1.MoviePlayer1” is hardcoded… and what i wanted is when this container is placed on window1 that it get its window instance so i could write something like
theWindowIAmPlacedOn.movieplayer1.position=dragvalue

so that i can place it on another window without hardcoding the window name…

The problem is the control MoviePlayer1 is on Window1 only so you can’t abstract this. When you use TrueWindow you get back a Window instance, not a Window1 instance. So you should cast it with Window1(TrueWindow). But this again will force you to hardcode Window1 in your Container control.

This would require an Interface, but again this is not exactly for beginners

Use events.

  1. In your container control class create a new event definition called SliderChanged with the parameter: pValue As Integer

  2. In your container control class where you calculate dragvalue, raise the SliderChanged event:
    dragvalue=currentPos*value/(bar.width-pointer.width)
    RaiseEvent SliderChanged(dragvalue)

  3. In your window find the container control, add the SliderChanged event and add the following code:
    window1.MoviePlayer1.Position = pValue

Thanks, Kevin it works in this particular piece of code but in general i need to know how to control other controls on the same window on which the container control was placed (without hardcoding). So i think i have to pass a reference (in this case window1) to my container control and that is where i am stuck.
As Massimo Valle wrote, i need an Interface but i don’t kow how to use them…

this is only because i want to make it reusable for other windows and i thought: if placed on window1 get me that window instance so i can address other controls like:
theWindowIAmPlacedOn.anOtherControlOnThatWindow.doWhatIWant

Although you have placed the container control onto a specific window your container cannot directly reference any methods, properties or controls specific to that window. This is because the container can be placed on any type of window class so writing code in the container to directly address a specific type of window class would not make any sense.

The best design is to implement the container control so that it does not care about the window it is placed on. Using events (like I suggested) or implementing the observer pattern could be two ways to do this.

If you just want the container control to work with multiple instances of the same window class you can do this easily. However, the window and container control will be tightly coupled so you couldn’t reuse the container control on other types of windows.
a) Add a property to your container control that is the same type as your window class.
b) Assign the property during some initialisation method in your window.
c) Use the property in your container control code to refer back to the window.
IMPORTANT. This will probably create a circular reference and leak memory when the window is closed. You can solve this by using a weakref property. You will also have to make sure that your container control code does not access the property too early (before the window has assigned it).
This could be made less tightly coupled by using an interface. However, you couldn’t access the controls directly and would need to implement some kind of event dispatcher method.