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?

 採用された回答

MathWorks Support Team
MathWorks Support Team 2009 年 6 月 27 日

0 投票

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.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

製品

リリース

R2010b

Community Treasure Hunt

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

Start Hunting!

Translated by