How do I reuse my context menu for multiple objects?

I've created a uicontextmenu object, that I want to use with multiple objects in my GUI. How do I program it to recognize which object I clicked on and perform the appropriate action, instead of programming a separate context menu for each object?

 Respuesta aceptada

The handle of the last object that you right-clicked (to open the context menu) in the figure will be stored in the figure's CurrentObject property. You can access this property directly with the GCO command, or by using GET on the main figure. You can then use the value of this property to determine what to do. Example:
function varargout = contextmenu_Callback(h, eventdata, handles, varargin)
% determine which object was clicked
obj = get(handles.figure,'CurrentObject');
% the following statement is equivalent when used in this context:
% obj = gco;
switch get(obj,'type')
case 'axes'
% code if axes was clicked
case 'uicontrol'
switch get(obj,'Style')
case 'pushbutton'
% right-clicked a pushbutton
case 'edit'
% right-clicked a edit text box
case 'text'
% right-clicked a static text box
end
end
You could also compare the handle for the object to the data stored in the 'handles' structure variable, to determine exactly which object was selected.

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Centro de ayuda y File Exchange.

Productos

Versión

R2010b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by