MenuItem.Popup Position in nested Containers

Hello,

I have different nested ContainerControls. One of them has a PushButton. After clicking on it I’d like to appear a MenuItem under the PushButton.

I Don’t want to use a BevelButton!

So the PopUp coordinates will be on PushButtons Top position + his height. While using TrueWindow to get the Windows correct position and add it to the PushButtons coordinates, it’s only working fine if a add one container directly to a window. But if a container owns some other containers with this PushButton, the position will be wrong. How can I every time get the correct position?

You’ll need to iterate through the parent objects until parent = Nil, adding all of the Lefts together.

Thanks Greg, could you please go a bit more into detail?

@Martin Trippensee — The following code displays an Edit contextual menu under a PushButton

[code]// Inside the PushButton.Action evvent

Dim popMenu As MenuItem
popMenu = EditMenu.Clone

Dim selectedMenu As MenuItem
selectedMenu = popMenu.Popup( me.Truewindow.Left + me.Left, me.TrueWindow.Top + me.Top + me.Height + 8 )[/code]

Stphane, thanks for your post. It’s exactly the code I use, but it does not work within nested ContainerControls.

@Martin Trippensee — Sorry, I used nested controls instead of nested ContainerControls. That brings us exactly to what Greg was talking about. Here is the code that works for me with nested ContainerControls.

[code]// In PushButton.Action
dim nx, ny as integer
dim rc as RectControl = me

nx = me.Left
ny = me.Top

while rc.Parent<>nil
rc = rc.Parent
nx = nx + rc.Left
ny = ny + rc.Top
wend

Dim popMenu As MenuItem
popMenu = EditMenu.Clone
call popMenu.Popup( me.Truewindow.Left + nx, me.TrueWindow.Top + ny + me.Height + 8 )[/code]

NOTE: the same code would run for any nested RectControl but it gives the right result only with embedded ContainerControls. You may want to add some type-checking

Thanks Stphane, this works like charm and always popups the menu to the right position.

@Martin Trippensee — You’re welcome. I just used what Greg was saying.