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

8 次查看(过去 30 天)
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?

采纳的回答

Avni Agrawal
Avni Agrawal 2024-7-30
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 个评论
Gavin
Gavin 2024-9-23
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!

请先登录,再进行评论。

更多回答(1 个)

Mario Malic
Mario Malic 2024-7-29
编辑:Mario Malic 2024-7-29
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

类别

Help CenterFile Exchange 中查找有关 Maintain or Transition figure-Based Apps 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by