How can I set a mouseclick callback function to UI controls created with app designer?

7 visualizaciones (últimos 30 días)
Hi,
I would like to set a callback function which executes when I click on uilistbox.
f2 = uifigure;
f2.WindowButtonDownFcn = @testCallback1;
list = uilistbox(f2);
list.Items = {'Red','Green','Blue'};
list.WindowButtonDownFcn = @testCallback2;
This code gives me an error as WindowButtonDownFcn apparently doesnt exist for uicontrols created with appdesigner(uifigure).
Is there any solution or callback function which I overlooked?
Thank you very much

Respuestas (1)

Aditya
Aditya el 21 de En. de 2025
Hi Tk,
In App Designer, UI controls such as "uilistbox" do not have a "WindowButtonDownFcn" property. Instead, you can use the "ValueChangedFcn" to respond to user interactions, such as selecting an item from the list. Here's how you can set it up:
% Create a UI figure
f2 = uifigure;
% Create a list box
list = uilistbox(f2);
list.Items = {'Red', 'Green', 'Blue'};
% Set the ValueChangedFcn callback
list.ValueChangedFcn = @(src, event) testCallback(src, event);
function testCallback(src, event)
% Callback function executed when the list box value changes
selectedValue = src.Value;
disp(['Selected: ', selectedValue]);
end
If you specifically want to detect mouse clicks on the list itself (without changing the selection), you need to set a callback on the figure, as uifigure supports mouse click callbacks. You can use WindowButtonDownFcn on the figure to detect mouse clicks:
% Create a UI figure
f2 = uifigure;
% Set the WindowButtonDownFcn callback
f2.WindowButtonDownFcn = @(src, event) figureClickCallback(src, event);
% Create a list box
list = uilistbox(f2);
list.Items = {'Red', 'Green', 'Blue'};
function figureClickCallback(src, event)
% Callback function executed when the figure is clicked
disp('Figure was clicked');
end
To detect clicks specifically on the list, you would need to handle this within the figure's callback, possibly by checking the position of the click relative to the list's position.
I hope this helps!

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by