Form.ShowDialog()或Form.ShowDialog(本)?Form、ShowDialog

2023-09-02 01:48:32 作者:北港初晴

我听说,如果我叫form.ShowDialog()不指定所有者,那么就可以当我将看不到屏幕上的对话框形式的情况下(这将与其他窗口隐藏)。是真的吗?我使用的ShowDialog()不指定所有者数百次,我从未有过的任何问题。

I heard that if I call form.ShowDialog() without specifying the owner, then there can be a case when I will not see the dialog form on screen (it will be hidden with other windows). Is it true? I used ShowDialog() without specifying the owner hundreds of times and I never had any problems with that.

能否请您解释一下在什么情况下我能得到所描述的问题?

Can you please explain in which situation I could get the described problem?

更新:

嗯,我做了很多的实验,我不能得到任何真正的意外问题,使用的ShowDialog()(不指定所有者)。

Well, I did many experiments and I couldn't get any real unexpected problems with using ShowDialog() (without specifying the owner).

所以,我认为这只是传言说的ShowDialog()会导致一些问题。 如果您不同意 - 给我一个code样品请注意,导致问题

So I think it's just rumors that ShowDialog() can lead to problems. If you don't agree - give me a code sample please that leads to a problem.

推荐答案

只是为了更好地了解业主拥有的关系:

Just to better understand the owner-owned relationship:

.NET允许一个形式为自己等形式。拥有形式是有用的   浮动工具箱和命令行窗口。一个拥有形式的一个例子是   查找并在Microsoft Word中替换窗口。当所有者窗口   最小化,所拥有的形式也自动最小化。当   资形式其所有者重叠,它总是显示在最前面。

.NET allows a form to "own" other forms. Owned forms are useful for floating toolbox and command windows. One example of an owned form is the Find and Replace window in Microsoft Word. When an owner window is minimized, the owned forms are also minimized automatically. When an owned form overlaps its owner, it is always displayed on top.

(三)临.NET 2.0的Windows窗体和自定义控件由马修·麦克唐纳。

(c) "Pro .NET 2.0 Windows Forms and Custom Controls" by Matthew MacDonald.

由于的ShowDialog 显示了新的形式,一个隐含的关系是   建立当前活动窗体之间,被称为老板   形式,而新的形式,被称为资形式。这种关系   确保拥有形式是活性形式和总是显示在   所有者窗体顶部。

As ShowDialog shows the new form, an implicit relationship is established between the currently active form, known as the owner form, and the new form, known as the owned form. This relationship ensures that the owned form is the active form and is always shown on top of the owner form.

这种关系的一个特征是,拥有形式影响   它的主人形式的行为(使用的ShowDialog ):

One feature of this relationship is that the owned form affects the behavior of its owner form (when using ShowDialog):

  在所有者窗体不能最小化,最大化,甚至感动。   的拥有形式块鼠标和键盘输入的所有者窗体。   在所有者窗体时所拥有的形式是最小化的。   只有拥有表单可以关闭。   如果既拥有者和拥有的形式被最小化,如果用户presses Alt + Tab键切换到资的形式,所有的形式被激活。    The owner form cannot be minimized, maximized, or even moved. The owned form blocks mouse and keyboard input to the owner form. The owner form is minimized when the owned form is. Only the owned form can be closed. If both owner and owned forms are minimized and if the user presses Alt+Tab to switch to the owned form, the owned form is activated.

与ShowDialog方法,但是,调用显示方式的做   不能建立一个隐含的所有者拥有的关系。这意味着   这两种形式可以是当前活动窗体。

Unlike the ShowDialog method, however, a call to the Show method does not establish an implicit owner-owned relationship. This means that either form can be the currently active form.

如果没有一个隐含的所有者拥有的关系,业主和拥有的形式   都可以被最小化,最大化,或移动。如果用户关闭任何   构成比的主要形式外,最近的活动形式是   重新激活。

Without an implicit owner-owned relationship, owner and owned forms alike can be minimized, maximized, or moved. If the user closes any form other than the main form, the most recently active form is reactivated.

尽管的ShowDialog建立一个隐含的所有者拥有的关系   没有内置的方式为独资形式的回调或查询   打开它的形式。在无模式的情况下,可以设置新   窗体的所有者财产建立所有者拥有的关系。作为一个   快捷键,你可以通过车主的形式作为参数传递给超载   Show方法,这也需要一个IWin32Window参数   (IWin32Window是由Windows实现窗体暴露UI对象   Win32的HWND通过IWin32Window.Handle property属性)。

Although ShowDialog establishes an implicit owner-owned relationship, there is no built-in way for the owned form to call back to or query the form that opened it. In the modeless case, you can set the new form's Owner property to establish the owner-owned relationship. As a shortcut, you could pass the owner form as an argument to an overload of the Show method, which also takes an IWin32Window parameter (IWin32Window is implemented by Windows Forms UI objects that expose a Win32 HWND property via the IWin32Window.Handle property).

的形式明确的模式所有者拥有的表单的行为   关系是相同的其隐式模态对应,但   无模式所有者拥有的关系提供了附加行为   非所有者拥有的无模式的情况下。首先,无模式资形式总是   出现在所有者窗体的顶部,尽管两者都可以被激活。   当你需要保持一种形式,如浮动工具这是非常有用   窗口上的其他形式的应用程序内的顶部。其次,如果   用户presses Alt + Tab在从所有者切换,所拥有的形式跟随   适合。为了确保用户知道哪些形式是主要形式,   最大限度地减少车主隐藏任务栏按钮为所有拥有的形式,   只留下主人的任务栏按钮可见。

The behavior of forms in an explicit modal owner-owned form relationship is the same as its implicit modal counterpart, but the modeless owner-owned relationship provides additional behavior in the non-owner-owned modeless case. First, the modeless owned form always appears on top of the owner form, even though either can be active. This is useful when you need to keep a form, such as a floating tool window, on top of other forms within an application. Second, if the user presses Alt+Tab to switch from the owner, the owned forms follow suit. To ensure that the user knows which form is the main form, minimizing the owner hides the task bar buttons for all owned forms, leaving only the owner's task bar button visible.

(三)的Windows Forms 2.0编程由克里斯·塞尔斯,迈克尔Weinhardt。

(c) "Windows Forms 2.0 Programming" by Chris Sells, Michael Weinhardt.