Is there a generic modal dialog for appdesigner that can be customized?
31 次查看(过去 30 天)
显示 更早的评论
I like the modal uialert and uiconfirm dialogs in appdesigner.
Is there a way to make custom dialogs that behave like those (that is, stick within the bounds of the UIFigure and darken it)
I would like to make dialogs like that but be able to add custom buttons, check boxes, pulldown menus, etc to them
3 个评论
回答(2 个)
Pranav Verma
2020-11-11
Hi Duijnhouwer,
From your query I understand that you need a way in MATLAB to create a customizable dialog box. Please refer to the below link which explains the functions and examples to create a customizable dialog box:
Thanks
RST
2024-2-20
编辑:RST
2024-5-9
Since R2020b we can make a custom AppDesigner modal dialog by using these features:
- app.UIFigure.WindowStyle = 'modal'
- uiwait() on our app.UIFigure
- a callback property in the app, and
- a function to create and wait on the app, with a nested function for the callback
This app is modal, has two buttons, and a ValueChangedFcn property. Here is a snip of the relevant AppDesigner code:
classdef modalApp1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Button2 matlab.ui.control.Button
Button1 matlab.ui.control.Button
end
properties (Access = public)
ValueChangedFcn function_handle {mustBeScalarOrEmpty(ValueChangedFcn)};
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.UIFigure.WindowStyle = 'modal';
end
% Button pushed function: Button1
function Button1Pushed(app, event)
if ~isempty( app.ValueChangedFcn)
app.ValueChangedFcn( 1 );
end
delete( app );
end
% Button pushed function: Button2
function Button2Pushed(app, event)
if ~isempty( app.ValueChangedFcn)
app.ValueChangedFcn( 2 );
end
delete( app );
end
end
The startupFcn sets the WindowStyle to be 'modal'. The button callbacks call the ValueChangedFcn callback to pass on the result and then delete() the app.
Now we want a function that displays the app, hooks into the callback and then waits for the app to close. It returns [] if the app is closed, or 1 or 2 according to the button pressed.
function result = showModalApp()
result = []; % result, set by nested funcion
hMA = modalApp1(); % show tha app
hMA.ValueChangedFcn = @valueChanged; % set nested function
uiwait(hMA.UIFigure); % wait for the app to close
function valueChanged( val )
% collect the result to a variable in the parent function
result = val;
end
end
This appears to be working for me in R2023a. I think it should work since R2020b when WindowStyle 'modal' was introduced.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!