How detect the removal of a contextual menu

I’ve got a problem to detect when a user abort a visible contextual menu by clicking outside the menu area. If the uses clicks in an other control in the window that takes over the focus, It’s possible to detect it in the lost focus event. But if the user clicks in an area that not removes the focus from the control that views the contextual menu, the menu will disappear without firing the lost focus event.
How do I detect the disappearing of the contextual menu when the control doesn’t loose the focus?

Sorry to ask but why would you need to know?
User doesnt want to do any of the options.

If the user must make a choice and you need to know they deliberately chose not to, you can either:
display a modal window
construct a menu in code and call .popup
If they cancel the popup, it returns a nil object

var m as new menuitem
var n as new menuitem
n.text = "click me'
m.append n

n = m.popup
if n = nil then
//user clicked somewhere else
//handle the choice
end if

Thanks for your answer Jeff.
I’m using a list box with multi selection.
When the user do a contextual click on a row in the list box, I want to show the user which row that is chosen with the contextual click independent of the normal selection in the list box. I’m doing that by changing the selection color. When the user aborts I must restore the original original selection color for the clicked row.

You could use a MenuItem and position it just below the clicked row.

You could use a MenuItem

Using the code above… :slight_smile:

One issue I ran into doing this over a listbox some time ago , is that if someone right-clicks on a listbox, it can be unreliable trying to identify the row in question.

You can use a custom class for one of the menuitems and then notice in the destructor that the menu got disposed.

1 Like

Thanks all.
I will try your suggestions.

Jeff: Did you solve the unreliability identifying the clicked row?
I’m using the list box row height to calculate the clicked row in the MouseDown event and not seen any issues so far.

I didnt solve it, but I was using rowfromXY , and if memory serves things were a problem because right clicking wasnt changing selection in time.

… which will be fine unless the list has scrolled

… and you also need to handle the header.
Below is the code I’m using in the mouseDown event:

var scrollposMem as integer = me.Scrollposition
var pos as Integer
if me.HasHeading = true then
pos = floor( ( y - me.headerHeight ) / me.RowHeight )
pos = floor( ( y ) / me.RowHeight )
end if
var clickedRowIx as integer = scrollposMem + pos


Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.