Problem with MessageBox
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?
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.
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!
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.
精彩评论