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

10 ビュー (過去 30 日間)
Tk
Tk 2019 年 5 月 2 日
回答済み: Aditya 2025 年 1 月 21 日 18:48
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

回答 (1 件)

Aditya
Aditya 2025 年 1 月 21 日 18:48
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!

カテゴリ

Help Center および File ExchangeDevelop uifigure-Based Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by