I’m on 2019r1.1 still so I don’t know if the following is still true.
If you walk the control hierarchy using RectControl.Parent, even if the the parent is a Container control, you can assign parent to a variable of type RectControl…
Which was fortuitous as i need to check up the hierarchy if any of the controls containing this one (whatever they might be) was a subclass that implemented a specific class interface.
Seeing that I thought great, that should mean that I can write an extension method to get the absolute coordinates for any control that can contain other controls for menu item.popup, regardless of if it was a RectControl or an embedded containerControl:
Public Sub AbsolutePosition(Extends Container as ContainerControl, byref X as integer, byref Y as Integer)
X = X + Container.Left
Y = Y + Container.Top
Do
If Container.Window IsA EmbeddedWindowControl Then
Container = ContainerControl(Container.Window )
X= X + Container.Left
Y = Y + Container.Top
Else
X= X + Container.Window.Left
Y = Y + Container.Window.Top
Return
End if
Loop
End Sub
But then when I tried to do that (me is a container control here):
me.AbsolutePosition(MenuX,MenuY) ' Does not compile
It did not compile So then i tried:
EmbeddedWindowControl(me).AbsolutePosition(MenuX,MenuY) 'illegal cast
It did compile (And autocomplete!!!) but when I ran I got an illegal cast exception
So next i tried:
RectControl(EmbeddedWindowControl(me)).AbsolutePosition(MenuX,MenuY)
And again it compiled but on run I got an illegal cast exception again!
So why can I assign the RectControl.Parent to a variable of type RectControl even when it is container control but the above not work!
I wound up creating an overload method to handle container controls but even THAT was confusing. I first defined the overload method as:
Public Sub AbsolutePosition(Extends Container as EmbeddedWindowControl, Byref X as integer, Byref Y as integer)
But then I got a compile error because the compiler could not distinguish between a rectControl and and an Embedded window! That REALLY surprised me given that
EmbeddedWindowControl(me).AbsolutePosition(MenuX,MenuY)
Did not work
This is what finally worked for the overload:
Public Sub AbsolutePosition(Extends Container as ContainerControl, Byref X as integer, Byref Y as integer)
This is VERY inconsistent behavior… Whatever magic Xojo inc choses to do under the hood should be consistent IMO!
I would report this inconsistency as a bug, but I am 99.9% sure I would be told this was by design .
-Karen