消息框问题

发布于 2024-09-24 13:12:35 字数 462 浏览 0 评论 0原文

我在使用模态消息框时遇到问题。

情况如下:

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

这是 MessageBox 的代码片段。

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

关于如何解决这个问题有什么想法吗?

I'm having a problem with a MessageBox intended to be modal.

Here is the situation,

  • A user select xx from the form
  • MessageBox appears
  • User opens the embebed software keyboard (the built-in one, from the device)
  • User closes the keyboard
  • The MessageBox loses focus (how? it's supossed to be modal!) and the main form is shown in the foreground
  • The app blocks, as the user cannot now close the MessageBox.

Here is the code snippet for the MessageBox.

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

Any ideas on how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伴我老 2024-10-01 13:12:35

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

当您单击桌面一角的 SIP 按钮时,整个应用程序将失去焦点,焦点将传递到任务栏。通过单击应用程序的任务栏按钮,您可以看到类似的“怪异” - 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 对操作系统的实际实现方式有很大的控制权,因此您永远不能排除平台行为错误。

This is actually somewhat expected behavior under Windows CE (I'm not saying it's right, just expected).

When you click on the SIP button down in the corner of the desktop, your entire app loses focus and focus is passed to the Task Bar. You can see similar "wierdness" by clicking on your application's Task Bar button - the MessageBox will lose focus, even though by all rights you should just be sending focus to the app that is already running.

You can see that it's not a CF bug by changing your MessageBox call like so:

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);

And you get the exact same behavior.

The one thing that is not expected is that the parent Form is coming up above the MessageBox. I just tested on an ARM-based CE 5.0 device I have on my desktop and the MessageBox stays on top in both the CF and the P/Invoke versions.

Are you able to repro this behavior with a very basic app (i.e. just one form, one button)? If so then it sounds like a platform issue. One thing to remember about using CE is that since the OEM has a lot of control over how the OS is actually implemented, you can never rule out a platform bug for behaviors.

Hello爱情风 2024-10-01 13:12:35

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

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

这里是一个链接请参阅 Microsoft 文档。

尽情享受吧!

You need to include a reference to the parent form when calling the MessageBox.Show (the IWin32Window parameter, usually just pass in "this"). I believe this is the overload you want to use - see below:

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

Here is a link to the Microsoft documentation.

Enjoy!

烏雲後面有陽光 2024-10-01 13:12:35
MessageBox.Show("Please insert Correct Username and Password.", "Login Error",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.Focus();

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

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

Its a simple solution. no need to run any JavaScript or other C# Code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文