显示带有所有者窗口的服务通知消息框不是有效的操作.使用不带所有者的Show方法



我试图在新表单的帮助下保持MessageBox在顶部。

MessageBox.Show(new Form { TopMost = true }, message, heading, buttonType, icon, MessageBoxDefaultButton.Button1, Options);

当我从一个窗体调用这个工作正常,但当我调用相同的方法使用子弹出窗体(父usercontrol的子),然后它抛出下面的异常。

Exception: Showing a service notification message box with an owner window is not a valid operation. Use the Show method that does not take an owner.

问题是option参数,我试图传递下面的选项,但都没有工作。

MessageBoxOptions.DefaultDesktopOnly
MessageBoxOptions.ServiceNotification

child (popProductionConfirmation)方法调用:

private void callMyMethod()
{
ShowMessageInMessageBox("Automatic Posting Done", "Production Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxOptions.DefaultDesktopOnly);
}
private void ShowMessageInMessageBox(string message, string heading, MessageBoxButtons buttonType, MessageBoxIcon icon, MessageBoxOptions Options)
{
CommonMethods.ShowMessageInMessageBox(message, heading, buttonType, icon, Options, this);
}
类CommonMethods与MessageBox:
public static void ShowMessageInMessageBox(string message, string heading, MessageBoxButtons buttonType, MessageBoxIcon icon, MessageBoxOptions Options, IWin32Window owner = null)
{
if (owner == null)
{
MessageBox.Show(new Form { TopMost = true }, message, "BTS: " + heading, buttonType, icon, MessageBoxDefaultButton.Button1, Options);
}
else
{
MessageBox.Show(owner, message, "BTS: " + heading, buttonType, icon, MessageBoxDefaultButton.Button1, Options);
}
}

父窗口和子窗口的创建:

popProductionConfirmation objpopProductionConfirmation;
objpopProductionConfirmation = new popProductionConfirmation();
objpopProductionConfirmation.ShowDialog();

那么,是否有任何方法将MessageBox保持在顶部或任何方法来验证该条件,或解决它?

错误告诉你所有你需要知道的,特别是这部分:

使用不带所有者的Show方法

查看框架的源代码:

if (owner != IntPtr.Zero && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)
{ 
throw new ArgumentException (SR.Get(SRID.CantShowMBServiceWithOwner)); 
}

它特别寻找一个条件,你提供了一个所有者,并使用你试图使用的标志之一,它抛出一个错误:

SRID.CantShowMBServiceWithOwner定义为:

显示带有所有者窗口的服务通知消息框不是有效的操作。使用不带所有者的Show方法

你看到的错误是什么

:

  • 使用Show()过载,将所有者作为参数,或者
  • 使用owner作为参数的而不是
  • 使用MessageBoxOptions参数。

保持消息框在顶部:

  • 如果使用owner,设置owner的TopMost属性为true
  • 如果使用无所有者过载,则使用MessageBoxOptions.DefaultDesktopOnly选项。

最新更新