Home > Back-end >  Winforms: Showing a dialog in the Load method of app's main form always shows to bottom
Winforms: Showing a dialog in the Load method of app's main form always shows to bottom

Time:04-17

Winforms .NET Framework 4.7.2

Hi - I have a program, and in the main form's load method I check some things on our cloud server, and even before that I check if there is internet. In several scenarios, I close the splash window, then put up a messagebox.show, later replaced with a class that emulates MessageBox with extra features.

This logic is in the Load method, and cannot be in shown method because information retrieved from the cloud determines things loaded into the main form's controls.

With either the Messagebox.Show or the emulated messageBox class, a showdialog is always on the bottom, even behind other apps running on the screen. Using TopLevel = true; in the emulator Class' shown event handler has no effect. Since this is the main form's load method, there is no handle to pass as owner to the ShowDialog method.

Is there any way to get this to the top?

Code called from the Load method:

            HandleCloseSplash()
            PTCommonControls.PTMessageBox.Show("Your PC is not connected to the Internet." & vbCrLf & "AutoUpdate™, MyActivate™ and other online features are disabled." & vbCrLf & "Please connect to the internet when using this program.",
                                                      "No Internet",
                                                      MessageBoxButtons.OK,
                                                      MessageBoxIcon.Information)

The Shown event handler from the PTMessageBox class:

                    flexibleMessageBoxForm.Shown  = (sender, e) =>
                    {
                        flexibleMessageBoxForm.TopLevel = true;
                        if (flexibleMessageBoxForm.Parent == null) { flexibleMessageBoxForm.ShowInTaskbar = true; }
                    };

Thanks for any help.

CodePudding user response:

Solved. Thanks to @Hans Passant, I was pointed in the correct direction. The solution was:

  1. Hide and not close the splash form (set Visible to false)
  2. Use the system-provided MessageBox class
  3. Set the "ServiceNotification" option in MessageBoxOptions.

This works perfectly.

Thanks!

  • Related