uiwait(msgbox("My message") dialog comes up behind the app. User never sees it.

8 views (last 30 days)
Why does uiwait(msgbox("My message") dialog come up behind the app? My user never sees it so can't click OK. Is there a way to bring it to the front other than uialert() which is a lot more complex to program?

Accepted Answer

Avni Agrawal
Avni Agrawal on 30 Jul 2024
Hi @Gavin,
I understand that the `msgbox` function can sometimes create a message box that appears behind the main app window, making it difficult for users to see and interact with it. To ensure that the message box appears in front of the app, you can use the `figure` function to bring the message box to the front.
Here's a simple way to ensure that the message box appears in front:
h = msgbox('My message');
figure(h);
uiwait(h);
This code snippet creates the message box and then brings it to the front using the `figure` function. The `uiwait` function will still block the execution until the user clicks OK.
Alternatively, if you want a more robust solution, you can use the `WindowStyle` property of the message box to make it modal, which ensures that the message box stays on top of the app until the user interacts with it:
h = msgbox('My message', 'Title', 'modal');
uiwait(h);
By setting the `WindowStyle` to `'modal'`, you make sure that the message box remains in front of the app window and requires the user to interact with it before proceeding.
Both of these methods should help ensure that your message box is visible to the user and can be interacted with appropriately.
I hope this helps!
  1 Comment
Gavin
Gavin on 23 Sep 2024
Great help. Sorry for such a late reply. I switched to uialert and uiconfirm in my app but this answer can be useful I hope for others and for MyDir = uigetdir(path,title) which pops up wherever it feels like!

Sign in to comment.

More Answers (1)

Mario Malic
Mario Malic on 29 Jul 2024
Edited: Mario Malic on 29 Jul 2024
Hi again,
I don't know if there is a better solution, but here is an example with uialert
Idea is to create a new figure with it, and once the button is pressed the figure will be deleted.
fig = uifigure();
% fig = uifigure("WindowStyle", "modal"); % Maybe this works better
uialert(fig,"File not found.","Invalid File", "CloseFcn", @(src, evt)delete(src));
while ishandle(fig) % wait for figure to be deleted
pause(0.1);
end
% rest of code
In case the figure shows behind the active window, try focus function, or
uifigure(fig);
Cheers

Categories

Find more on Maintain or Transition figure-Based Apps in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!