The World’s Largest Online Community for Developers
I have a borderless Windows Forms application.
The main window creates other forms (simple dialogs where I can click yes or no) with
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.
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 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 half a day refactoring ;)
Have you tried passing a reference to the main window to
// 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.