Determine what UI Object Called a Context Menu
11 views (last 30 days)
This seems like a pretty basic question.
I have a pretty complex App created in App Designer. I have finally finished all of my basic functions I want it to do, but now I need to add some refinement and user customizations. I want to be able to do some of this via context menus.
I have 18 listboxes in my app. Each one can hold an arbitrary number of items that correspond to data variables used in the background and on plotting. I want to create ONE context menu that can be used on all 18 listboxes rather than creating 18 context menus.
Ideally, I'd love to be able to pass exactly which listbox item has been clicked on, BUT, I've seen that question already asked here: https://www.mathworks.com/matlabcentral/answers/789804-contextmenu-for-each-listboxitem without an answer.
All I really need to enable that is to be able to pass WHICH listbox the context menu was spawned from to the callback function. From there, I can have it do what I need. However, I looked through the documentation here: https://www.mathworks.com/help/matlab/ref/uicontextmenu.html and here: https://www.mathworks.com/help/matlab/ref/matlab.ui.container.contextmenu-properties.html and I looked at the event.Source object, and I can't seem to figure out how to get this information. The Parent object is the main UIFigure of the App itself, so that's way too high of a level to figure out which block the context menu was used in. It seems, since it's a context menu, that it should know the context in which it was spawned. How can I get this information to the callback? I really don't want to create 18 different context menus.
I guess I could figure out how do it programmatically... but then I can't use the GUI editor for it.
I feel like there should be an easier way - like a property I could check. If so, what is that property, or what kind of method / function could I use to identify the UI object that the menu was called from?
Kevin Holly on 9 Jan 2023
Edited: Kevin Holly on 9 Jan 2023
Could you create a property variable that stores the handle of the last clicked listbox?
something such as:
app.listbox1.ClickedFcn = 'app.selected = app.listbox1'
app.listbox2.ClickedFcn = 'app.selected = app.listbox2'
app.listbox3.ClickedFcn = 'app.selected = app.listbox3'