Windows Forms和ShowDialog的问题问题、Windows、Forms、ShowDialog

2023-09-03 03:56:25 作者:致命非爱i

我有一个无国界的 Windows窗体应用程序

在主窗口中创建了其他形式(简单的对话,我可以单击是或否)与的ShowDialog()。 每个创建的对话是不可见的在任务栏,我的应用程序只有一个,专注我的应用程序的任务栏条目(如果对话是开放的,一个是重点)。如果我使用 ALT + TAB 循环到所有打开的窗口我只看到一个条目了。

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.

如果我点击任务栏上的关注我的应用程序,在主窗口集中(而不是对话框)。 我不能使用主窗口(因为还有一个开放的模式对话框)。 在Windows 7的 ALT + TAB preVIEW显示对话框,而任务栏鼠标悬停preVIEW显示主窗口(在正常的行为都显示在对话框前面的主窗口的)。 来让我的应用程序再次可用,唯一的办法就是 ALT + TAB 的输入并关闭模态对话框。 如果我用 ALT + TAB 只有对话框带到前面,主窗口是仍然在后台。 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.

有没有一种方法,以prevent这种情况发生? 我知道该怎么做,但大多数客户认为应用程序崩溃,因为主窗口没有响应。

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.

更新:

解决方案是通过顶层窗口的的ShowDialog()方法(在大多数情况下,如果使用的形式,这将是本)。

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").

由于我没有想重构我的整个code,和我所有的形式从MyCustomFormBase继承这里是一个小的解决方案,工作得很好。

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 ;)

推荐答案

你试过传递引用到主窗口,以的ShowDialog 电话?

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:

该版本ShowDialog方法的   允许您指定一个特定的形式   或控制将拥有的对话框   框被示出。如果使用   版本这种方法具有没有   参数,被所示的对话框   将由自动资   当前活动窗口中的   应用程序。

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.