MenuItem.Popup Position in nested Containers

  1. last week

    Martin T

    Aug 9 Pre-Release Testers Germany

    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?

    @MartinTrippensee — 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.

    // 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 )

    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

  2. Greg O

    Aug 9 Xojo Inc

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

  3. Martin T

    Aug 9 Pre-Release Testers Germany

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

  4. @MartinTrippensee — The following code displays an Edit contextual menu under a PushButton

    // 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 )
  5. Martin T

    Aug 9 Pre-Release Testers Germany
    Edited last week

    Stéphane, thanks for your post. It's exactly the code I use, but it does not work within nested ContainerControls.

  6. Stéphane M

    Aug 9 Answer France

    @MartinTrippensee — 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.

    // 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 )

    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

  7. Martin T

    Aug 9 Pre-Release Testers Germany

    Thanks Stéphane, this works like charm and always popups the menu to the right position.

  8. @MartinTrippensee — You're welcome. I just used what Greg was saying.

or Sign Up to reply!