C#/.NET 消息框不是模态的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1154181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# / .NET messagebox is not modal
提问by emeh
Why is a C#/.NET message box not modal?
为什么 C#/.NET 消息框不是模态的?
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).
偶然地,如果消息框在我们的主 UI 后面,那么主 UI 不会响应,直到我们单击确定(在我们的消息框上)。
Is there a workaround other than creating a custom message box?
除了创建自定义消息框之外,还有其他解决方法吗?
采纳答案by Charlie
You need to assign the MessageBox owner property to the main UI window (look at the 3rd constructor).
您需要将 MessageBox owner 属性分配给主 UI 窗口(查看第三个构造函数)。
回答by Justin Niessner
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.
模态弹出窗口在技术上被定义为一个弹出框,它会中断应用程序的正常流程......不一定是停留在所有其他窗口的顶部,因此您所描述的行为对于模态弹出窗口是正确的.
Here's a project on CodeProject that tries to mimic the "always on top" functionality for a MessageBox style Modal window:
这是 CodeProject 上的一个项目,它试图模仿 MessageBox 样式的模态窗口的“始终在最前面”功能:
回答by Dusty
You can use the owner parameter to specify a particular object, which implements the IWin32Window interface, to place the message box in front of.
您可以使用 owner 参数来指定一个特定的对象,该对象实现了IWin32Window 接口,将消息框放在前面。
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.
消息框是一个模态对话框,这意味着除了模态窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。程序必须隐藏或关闭模态表单(通常是为了响应某些用户操作),然后才能输入另一个表单。
回答by JamesM
This is a simple C# new Windows Forms application:
这是一个简单的 C# 新 Windows 窗体应用程序:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string message = "You did not enter a server name. Cancel this operation?";
string caption = "No Server Name Specified";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
// Displays the MessageBox.
result = MessageBox.Show(this, message, caption, buttons);
if (result == DialogResult.Yes)
{
// Closes the parent form.
this.Close();
}
}
}
}
As Dusty states in his answer, a message box is a modal dialog. Specify the 'owner' property. In this example, the owner is denoted by the keyword 'this'.
正如Dusty 在他的回答中所说,消息框是一个模态对话框。指定“所有者”属性。在此示例中,所有者由关键字“this”表示。
回答by JamesM
public static System.Windows.Forms.DialogResult WW_MessageBox(string Message, string Caption,
System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon,
System.Windows.Forms.MessageBoxDefaultButton defaultButton)
{
System.Windows.Forms.MessageBox.Show(Message, Caption, buttons, icon, defaultButton,
(System.Windows.Forms.MessageBoxOptions)8192 /*MB_TASKMODAL*/);
}
回答by Max
Make the message box appear in the main thread, if your form has been created from it:
如果您的表单是从它创建的,则使消息框出现在主线程中:
private bool ShowMessageBoxYesNo()
{
if (this.InvokeRequired)
return (bool)this.Invoke(new ShowMessageBoxYesNoDelegate(ShowMessageBoxYesNo));
else
{
DialogResult res = MessageBox.Show("What ?", "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (res == DialogResult.Yes)
return true;
else
return false;
}
}
回答by user1801179
To get system modal messagebox set MessageBoxOptions.DefaultDesktopOnly.
要获取系统模式消息框设置MessageBoxOptions.DefaultDesktopOnly。
回答by surya
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.
MessageBox 是服务器本地的本地控件。直到单击服务器中显示的消息框上的“确定”才会响应。
回答by Farrukh Waheed
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.
Form.ActiveForm 将为您提供当前活动的表单,即使您是从任何其他类提升 MessageBox 也是如此。
回答by Roberto
What I usually do if I have to trigger a MessageBox from another thread (not from the main thread) is this:
如果我必须从另一个线程(而不是从主线程)触发 MessageBox,我通常会做的是:
I create a static variable with the form instance:
private static Form1 myform;
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); });
我用表单实例创建了一个静态变量:
私有静态 Form1 myform;
从线程中,我调用操作以显示主线程中的 MessageBox:
myform.BeginInvoke((MethodInvoker)delegate() { MessageBox.Show("进程完成!", "线程进程信息", MessageBoxButtons.OK, MessageBoxIcon.Information); });
This is just a "cookie-cutter" that I always use, and works perfectly for me.
这只是我经常使用的“千篇一律”,非常适合我。