App Designer - confirm exit when application is closed from Windows bar

Hi guys,
I have a simple question regarding App Designer.
Sometimes I accidentally click the close button which exits the application (obviously). Is there a way to implement the confirmation dialog?

 Accepted Answer

Add a close request function to your app that requires confirmation.
1. From AppDesigner > Design View, right click the figure background, go down to "Callbacks", and select "Add CloseRequestFcn function callback".
2. A UIFigureCloseRequest() function will be added in Code View. Add this to the function where app.UIFigure is the handle to your app figure.
YN = uiconfirm(app.UIFigure,'Do you want to close the app?', 'Close request');
if strcmpi(YN,'OK')
delete(app)
end

11 Comments

Works great, I have not seen this "almost hidden" menu before.
Thank you Adam!
You can also access the figure's callback functions by clicking on the figure handle in the Component Browser and then click "callbacks" in the lower half of the panel on the right.
I didn't use callbacks for the figure before,and I didn't even know that they exist. Thanks for the hint.
@Adam Danz Thanks a lot for this Answer. It helped me too.
That's a help, indeed, but is there a way to disable the window 'Close' "X" (and Alt-F4) programmatically to prevent the user from doing so while the app is busy doing something that could result in files left open, etc., ...? Can (and do) disable the programmed "Quit" button when beginning processing and then reenable it when done, but would be agoodthing™ to be able to short-circuit an accidental attempt otherwise, because they could still respond 'Yes' to the prompt...although I guess one could test a status flag in the callback function and refuse is that were set...
@dpb what if you added the close request callback and simply had it return without doing anything
return; % Bail out without closing.
I don't think it would get rid of the little x in the upper right of the title bar though. However, if you set the 'WindowState' property of the figure to 'fullscreen', it should not have a title bar and should cover up the task bar at the bottom.
app.UIFigure.WindowState = 'fullscreen';
Yeah, @Image Analyst, that's what I've done for the moment with further amplification in the other thread on the callbacks in the particular app being confounded by my prior actions, not knowing better at the time...
This was such a great question that I wrote an article about it this morning.

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Products

Release

R2020a

Asked:

on 12 Sep 2020

Commented:

on 29 Apr 2025

Community Treasure Hunt

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

Start Hunting!