@manhng

Welcome to my blog!

Lost focus after showing MessageBox

February 5, 2020 10:39

MessageBoxOptions (edit)

Remove the MessageBoxOptions.DefaultDesktopOnly parameter and it will work correctly.

DefaultDesktopOnly specifies that "The message box is displayed on the active desktop" which causes the focus loss.

https://stackoverflow.com/questions/15901047/c-windows-form-messagebox-on-top-not-working

Given an instance of your Form, you can call a MessageBox like this:
MessageBox.show(form, "Message", "Title"); (Check the doc for other parameters.)

However if you want to call this from a background thread (e.g.: BackgroundWorker) you have to use Form.Invoke() like this:

form.Invoke((MethodInvoker)delegate
{
   MessageBox.show(form, "Message", "Title");
});

https://stackoverflow.com/questions/39387630/messagebox-not-showing-in-winforms-application-c-sharp

MessageBox.Show(this, "Message", "Caption", MessageBoxButtons.OK);

https://stackoverflow.com/questions/41022980/have-a-message-box-show-on-top-of-installer-or-after-application-closes

I got the message box to show on top of all other applications by calling it like so:

                    MessageBox.Show(
                        "Server registered successfully!", 
                        "Gyptech Micrometer OPC Server",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information,
                        MessageBoxDefaultButton.Button1,
                        MessageBoxOptions.DefaultDesktopOnly);

The vital part being that last parameter, MessageBoxOptions.DefaultDesktopOnly. Now the box shows over top of the installer : )

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.messageboxoptions?view=netframework-4.5#System_Windows_Forms_MessageBoxOptions_DefaultDesktopOnly

MessageBox.Show(index.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);

https://stackoverflow.com/questions/16858480/messagebox-not-showing-focused-after-savefiledialog

MessageBox.Show(new Form() { WindowState = FormWindowState.Maximized, TopMost = true }, "You clicked Cancel button", "Cancel");

https://www.c-sharpcorner.com/UploadFile/mahesh/understanding-message-box-in-windows-forms-using-C-Sharp/

https://bettersolutions.com/csharp/syntax/messagebox.htm

https://www.thevbprogrammer.com/VB2010_03/03-02-Msgbox.htm

https://info.sapien.com/index.php/guis/gui-controls/spotlight-on-the-messagebox-control

https://social.msdn.microsoft.com/Forums/windows/en-US/431b2212-4c05-4dac-adf4-47eee04a8c16/form-losing-focus-after-messageboxshowquotquot?forum=winforms

https://www.infragistics.com/community/forums/f/ultimate-ui-for-windows-forms/58900/lost-focus-after-showing-messagebox-in-ultranumericeditor

Show the Wait Cursor

http://www.csharp411.com/the-proper-way-to-show-the-wait-cursor/

https://social.msdn.microsoft.com/Forums/en-US/2f44ae5a-e427-4a48-b2b5-fea0709f7043/change-mouse-cursor-in-a-c-windows-form?forum=csharplanguage

https://stackoverflow.com/questions/1568557/how-can-i-make-the-cursor-turn-to-the-wait-cursor

You can use Cursor.Current.

// Set cursor as hourglass
Cursor.Current = Cursors.WaitCursor;

// Execute your time-intensive hashing code here...

// Set cursor as default arrow
Cursor.Current = Cursors.Default;

However, if the hashing operation is really lengthy (MSDN defines this as more than 2-7 seconds), you should probably use a visual feedback indicator other than the cursor to notify the user of the progress. For a more in-depth set of guidelines, see this article.

Edit:
As @Am pointed out, you may need to call Application.DoEvents(); after Cursor.Current = Cursors.WaitCursor; to ensure that the hourglass is actually displayed.

Categories

Recent posts