Handle of UI component from which a context menu has been opened
Show older comments
Dear all,
I spent the last hour looking for a solution to this problem but I've not found anything useful. I have a very simple problem, and probably the solution is easy as well: I have a context menu shared among several UI components. How can I get an handle of the object for which the context menu has been opened? I can't use different context menus because the UI components are created interactively.
EDIT: I try to explain a little bit better my issue. I have a GUI created with App Designer. The GUI shows an image on which the user can draw several ROIs (line, boxes, etc.). The same context menu is attached on each ROI and contains two items only: rename and delete object. My point is: when I click on "rename" how can I get the handle of the element from which the context menu has been opened? I tried to setup the "ContextMenuOpeningFcn" but it doesn't work (the function is not called when the menu is opened).
Thank you for your help!
Accepted Answer
More Answers (2)
Jasmine Poppick
on 20 Sep 2023
Starting in R2023b, the MenuSelectedFcn and ContextMenuOpeningFcn callback event data contains the UI component that was right-clicked to open the context menu, stored in the ContextObject property.
For example, if you run this code and open the context menu on the button, the callback displays the Button object.
fig = uifigure;
cm = uicontextmenu(fig,ContextMenuOpeningFcn=@(src,event)disp(event.ContextObject));
m = uimenu(cm,Text="Menu");
btn = uibutton(fig,ContextMenu=cm);
Paola De Cecco
on 23 Jan 2023
0 votes
One alternative could be to set UserData on all the context menus to the event Source parent.CurrentObject.
then you can read from UserData of each of the context menu, the handle of the uicontrol that created the context menu.
function ContextMenuOpening(app, event)
disp('can I get the menu owner here?')
L=length(event.Source.Children)
for i=1:L
set(event.Source.Children(i),'UserData',event.Source.Parent.CurrentObject);
end
end
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!