Problems with Windows Forms and ShowDialog

I have a limitless Windows Forms application .

The main window creates other forms (simple dialogs in which I can click "Yes" or "No") using ShowDialog() . Each dialog box created does not appear on the taskbar, in my application there is only one entry in the taskbar that focuses my application (and if the dialog is open and the other is focused). If I use ALT + TAB for the loop for all open windows, I see only one entry.

However, if the dialog is created while my application has no focus (for example, the user starts a long-term task, starts working on something else and, being in the background, my application displays the "Task done ..." dialog box), and I I want to return to my application, everything becomes strange.

  • If I click on the taskbar to focus my application, the main window focuses (not the dialog).
  • I cannot use the main window (because there is still an open modal dialog).
  • Preview Windows 7 ALT + TAB shows a dialog box, while a preview on the taskbar on the taskbar shows the main window (in normal mode, both show a dialog in front of the main window).
  • The only way to use my application again is ALT + TAB to enter and close the modal dialog.
  • If I use ALT + TAB , only the dialog is brought to the fore, and the main window is still in the background.

Is there any way to prevent this? I know what to do, but most customers think that the application crashed because the main window is not responding.

Update:

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

Since I did not want to reorganize all my code, and all my forms inherited from "MyCustomFormBase", here is a small solution that works very well.

 Public Class MyCustomFormBase Public Shared Property ApplicationMainForm() As Form Get 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) Else 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 for a half day refactoring;)

+6
winforms showdialog
source share
1 answer

Have you tried passing the link 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 for this overload :

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

+4
source share

All Articles