MessageBox的问题

时间:2010-09-17 18:53:06

标签: c# windows-mobile compact-framework

我遇到了一个有意为模态的MessageBox的问题。

情况如下,

  • 用户从表单
  • 中选择xx
  • MessageBox出现
  • 用户打开嵌入式软件键盘(内置设备)
  • 用户关闭键盘
  • MessageBox失去焦点(怎么样?它是模态的!)并且主窗体显示在前景中
  • 应用程序阻止,因为用户现在无法关闭MessageBox。

以下是MessageBox的代码段。

MessageBox.Show("message", "caption", MessageBoxButtons.OK, MessageBoxIcon.Asterisk,
                                    MessageBoxDefaultButton.Button1);

关于如何解决这个问题的任何想法?

3 个答案:

答案 0 :(得分:1)

这实际上是Windows CE下的预期行为(我不是说它是正确的,只是预期)。

当您单击桌面一角的SIP按钮时,整个应用程序将失去焦点,焦点将传递到任务栏。通过单击应用程序的任务栏按钮,您可以看到类似的“wierdness” - MessageBox将失去焦点,即使所有权利都应该将焦点发送到已经运行的应用程序。

您可以通过更改MessageBox调用来查看它不是CF错误:

private void button1_Click(object sender, EventArgs e)
{
    //MessageBox.Show("message", "caption", MessageBoxButtons.OK, 
    //                                    MessageBoxIcon.Asterisk,
    //                                    MessageBoxDefaultButton.Button1);

    MessageBoxCE(this.Handle, "message", "caption", 0);
}

// renamed to not collide with the Windows.Forms MessageBox class
[DllImport("coredll", EntryPoint="MessageBox")]
private static extern int MessageBoxCE(IntPtr hWnd, string lpText, 
                                       string lpCaption, int Type);

你得到完全相同的行为。

期望的一件事是父表单出现在MessageBox之上。我刚刚在桌面上的基于ARM的CE 5.0设备上进行了测试,而MessageBox在CF和P / Invoke版本中都保持领先。

您是否可以使用非常基本的应用程序(即只有一个表单,一个按钮)重现此行为?如果是这样,那么它听起来像一个平台问题。关于使用CE,要记住的一件事是,由于OEM对操作系统的实际实现方式有很多控制权,因此您永远不能排除行为的平台错误。

答案 1 :(得分:0)

在调用MessageBox.Show时,您需要包含对父窗体的引用(IWin32Window参数,通常只传入“this”)。我相信这是你想要使用的超载 - 见下文:

MessageBox.Show Method (IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton)

Here is a link to the Microsoft documentation.

享受!

答案 2 :(得分:0)

MessageBox.Show("Please insert Correct Username and Password.", "Login Error",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.Focus();

这是一个简单的解决方案。无需运行任何JavaScript或其他C#代码。

相关问题