C# / .NET messagebox is not modal

2019-01-17 14:05发布

Why is a C#/.NET message box not modal?

Accidentally, if the message box goes behind our main UI, then the main UI doesn't respond, until we click OK (on our message box).

Is there a workaround other than creating a custom message box?

10条回答
做自己的国王
2楼-- · 2019-01-17 14:25

What I usually do if I have to trigger a MessageBox from another thread (not from the main thread) is this:

  1. I create a static variable with the form instance:

    private static Form1 myform;

  2. From the thread, I invoke the operation to show the MessageBox from the main thread:

    myform.BeginInvoke((MethodInvoker)delegate() { MessageBox.Show("Process finished!", "Thread Process Information", MessageBoxButtons.OK, MessageBoxIcon.Information); });

This is just a "cookie-cutter" that I always use, and works perfectly for me.

查看更多
萌系小妹纸
3楼-- · 2019-01-17 14:29

You can use the owner parameter to specify a particular object, which implements the IWin32Window interface, to place the message box in front of.

A message box is a modal dialog, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

MessageBox.Show Method

查看更多
我命由我不由天
4楼-- · 2019-01-17 14:30

To get system modal messagebox set MessageBoxOptions.DefaultDesktopOnly.

查看更多
Explosion°爆炸
5楼-- · 2019-01-17 14:33

A modal pop-up is technically defined as a pop-up box that interrupts the normal flow of the application...not necessarily one that stays on the top of all other windows so the behavior you're describing is correct for a modal popup.

Modal Window

Here's a project on CodeProject that tries to mimic the "always on top" functionality for a MessageBox style Modal window:

CodeProject: TopMost MessageBox

查看更多
甜甜的少女心
6楼-- · 2019-01-17 14:34

This is working for me:

MessageBox.Show(Form.ActiveForm,"Finished processing", "Process finished", , MessageBoxButtons.OK, MessageBoxIcon.Information);

Form.ActiveForm would give you the currently active form, even if you are raising your MessageBox from any other class.

查看更多
对你真心纯属浪费
7楼-- · 2019-01-17 14:37

MessageBox is a local control which is local for the server. And it does not respond until clicking OK on the message box which is displayed in the server.

查看更多
登录 后发表回答