The World’s Largest Online Community for Developers

'; .net - Windows Forms and ShowDialog problem - LavOzs.Com

I have a borderless Windows Forms application.

The main window creates other forms (simple dialogs where I can click yes or no) with ShowDialog(). Every created dialog is not visible in the taskbar, my application has only one taskbar entry that focuses my application (and if a dialog is open that one is focused). If I use ALT + TAB to cycle to all open windows I only see one entry, too.

However, if the dialog is created while my application doesn't have the focus (for example the user starts a long running task, starts to work on something else and while being in the background, my application shows a dialog "Task done...") and I want to go back to my application, things are getting strange.

  • If I click on the taskbar to focus my application, the main window is focused (not the dialog).
  • I can't use the main window (because there is still an open modal dialog).
  • Windows 7 ALT + TAB preview shows the Dialog while the taskbar mouseover preview shows the main window (in normal behavior both show the dialog in front of the main window).
  • The only way to make my application usable again is to ALT + TAB to the entry and close the modal dialog.
  • If I use ALT + TAB only the dialog is brought to the front and the main window is still in the background.

Is there a way to prevent that from happening? I know what to do, but most customers think the application crashed since the main window doesn't respond.


The solution is to pass the top level window to the ShowDialog() method (in most cases and if used in a form that would be the "this").

Since I didn't wanted to refactor my entire code, and all my forms inherit from "MyCustomFormBase" here is a little solution that works very well.

Public Class MyCustomFormBase

    Public Shared Property ApplicationMainForm() As Form
            Return _applicationMainform
        End Get
        Set(ByVal value As Form)
            _applicationMainform = value
        End Set
    End Property
    Private Shared _applicationMainform As Form

    Public Shadows Function ShowDialog() As DialogResult
        If MyCustomFormBase.ApplicationMainForm IsNot Nothing Then
            Return MyBase.ShowDialog(MyCustomFormBase.ApplicationMainForm)
            Return MyBase.ShowDialog()
        End If
    End Function

    Public Shadows Function ShowDialog(ByVal owner As IWin32Window) As DialogResult
        Return MyBase.ShowDialog(owner)
    End Function

End Class

In the constructor of the main window I use

MyCustomFormBase.ApplicationMainForm = Me

once. It helped me half a day refactoring ;)

Have you tried passing a reference to the main window to ShowDialog calls?

// assuming this code is in the main form (so "this" refers to the main form)
DialogForm dialog = new DialogForm();
DialogResult result = dialog.ShowDialog(this);

Quote from the documentation of this overload:

This version of the ShowDialog method allows you to specify a specific form or control that will own the dialog box that is shown. If you use the version of this method that has no parameters, the dialog box being shown would be owned automatically by the currently active window of your application.

Best way to hide a window from the Alt-Tab program switcher?
How can I prevent focus change when ShowDialog exits?
Modal Dialog not showing on top of other windows
Dialog Window Gets Lost Behind Other Windows
Stop ShowDialog from removing focus from the parent form
Dialog in a Dialog window - Parent dialog closes with child dialog
winform ShowDialog(this) still fail
Windows Forms - ShowDialog with a parent - why doesn't clicking on the parent activate the dialog when main window is maximized