Search code examples
c#winformsshowdialog

Winforms ShowDialog(this) blocks the switching windows of other applications to the foreground


When I open any dialog in my Winforms application then Windows 10 behaves oddly in these ways:

  1. ALT-TAB no longer displays the list of windows
  2. I cannot switch to hidden applications using taskbar icons - if I select a taskbar icon of a window that is not currently visible (even if it is not hidden by the winforms app) the icon just flashes and then stays highlighted, but is not brought to the foreground. I can use other applications that are visible. As soon as I close the dialog form the other I can use the windows of other applications correctly
  3. If I switch to the application window that is behind the winforms application by clicking on it, I cannot go back to the winforms app either by ALT-TAB or by clicking on the taskbar icon. I can get back to it by minimizing the other application

I'm opening the dialogs with

dialogFormName.ShowDialog(this);

TopMost is set false on all forms and is not set in the code.

I've read about 50 related articles and the only problems seem to be either TopMost is set or ShowDialog is not called with the parent form. I'm not a regular Winforms developer so I'm probably doing something stupid. This is driving me crazy, so I'd really appreciate any help!

Edit: The same issues occur with MessageBox.Show(this, "test"). The issue does not occur with a newly created app just with a single button calling MessageBox.Show(this, "test"). The problem application uses EntityFramework but no other packages and the problem existed before I added EF.


Solution

  • After trying different scenarios I eventually found the issue. In my case I was calling ShowDialog() after a user clicks an item on a ContextMenu. The blocking of ALT-TAB was caused by the following code that attached the ContextMenu to the ListView that the menu was contextually for:

    lstList.ContextMenu = myContextMenu;
    

    As soon as I removed that association, the ShowDialog no longer blocked ALT-TAB.