Form.ShowDialog Form.ShowDialog Form.ShowDialog Form.ShowDialog Method

定义

将窗体显示为模式对话框。Shows the form as a modal dialog box.

重载

ShowDialog() ShowDialog() ShowDialog() ShowDialog()

将窗体显示为模式对话框。Shows the form as a modal dialog box.

ShowDialog(IWin32Window) ShowDialog(IWin32Window) ShowDialog(IWin32Window) ShowDialog(IWin32Window)

将窗体显示为具有指定所有者的模式对话框。Shows the form as a modal dialog box with the specified owner.

ShowDialog() ShowDialog() ShowDialog() ShowDialog()

将窗体显示为模式对话框。Shows the form as a modal dialog box.

public:
 System::Windows::Forms::DialogResult ShowDialog();
public System.Windows.Forms.DialogResult ShowDialog ();
member this.ShowDialog : unit -> System.Windows.Forms.DialogResult
Public Function ShowDialog () As DialogResult

返回

异常

要显示的窗体已经可见。The form being shown is already visible.

-or- 所显示窗体被禁用。The form being shown is disabled.

- 或 --or- 显示的窗体不是顶级窗口。The form being shown is not a top-level window.

- 或 --or- 显示为对话框的窗体已经是模式窗体。The form being shown as a dialog box is already a modal form.

- 或 --or- 当前进程不是以用户交互模式运行的(有关更多信息,请参见 UserInteractive)。The current process is not running in user interactive mode (for more information, see UserInteractive).

示例

下面的示例将窗体显示为模式对话框,并在确定是否在对话框窗体上读取 TextBox 控件的值之前计算对话框的返回值。The following example displays a form as a modal dialog box and evaluates the return value of the dialog box before determining whether to read the value of a TextBox control on the dialog box form. 此示例要求创建一个名为 @no__t 的 @no__t 0,其中包含一个名为 TextBox 的 @no__t 控件。This example requires that a Form named testDialog is created and that it contains a TextBox control named TextBox1. 此外,该示例还要求包含本示例中的代码,并从不同 Form 进行调用,以便将 @no__t 显示为模式对话框。Furthermore, the example requires that code in this example is contained and called from a different Form in order to display testDialog as a modal dialog box. 该示例使用为对话框指定所有者的 @no__t 的版本。The example uses the version of ShowDialog that specifies an owner for the dialog box.

void ShowMyDialogBox()
{
   Form2^ testDialog = gcnew Form2;
   
   // Show testDialog as a modal dialog and determine if DialogResult = OK.
   if ( testDialog->ShowDialog( this ) == ::DialogResult::OK )
   {
      
      // Read the contents of testDialog's TextBox.
      this->txtResult->Text = testDialog->TextBox1->Text;
   }
   else
   {
      this->txtResult->Text = "Cancelled";
   }

   delete testDialog;
}
public void ShowMyDialogBox()
{
   Form2 testDialog = new Form2();

   // Show testDialog as a modal dialog and determine if DialogResult = OK.
   if (testDialog.ShowDialog(this) == DialogResult.OK)
   {
      // Read the contents of testDialog's TextBox.
      this.txtResult.Text = testDialog.TextBox1.Text;
   }
   else
   {
      this.txtResult.Text = "Cancelled";
   }
   testDialog.Dispose();
}
Public Sub ShowMyDialogBox()
    Dim testDialog As New Form2()
    
    ' Show testDialog as a modal dialog and determine if DialogResult = OK.
    If testDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
        ' Read the contents of testDialog's TextBox.
        txtResult.Text = testDialog.TextBox1.Text
    Else
        txtResult.Text = "Cancelled"
    End If
    testDialog.Dispose()
End Sub

注解

您可以使用此方法在应用程序中显示模式对话框。You can use this method to display a modal dialog box in your application. 如果调用此方法,则在对话框关闭后才会执行它后面的代码。When this method is called, the code following it is not executed until after the dialog box is closed. 可以通过将 DialogResult 枚举的值之一分配给窗体上 Button 的 @no__t 属性,或在代码中设置窗体的 DialogResult 属性,来为该对话框分配一个值。The dialog box can be assigned one of the values of the DialogResult enumeration by assigning it to the DialogResult property of a Button on the form or by setting the DialogResult property of the form in code. 此方法随后将返回此值。This value is then returned by this method. 您可以使用此返回值确定如何处理对话框中发生的操作。You can use this return value to determine how to process the actions that occurred in the dialog box. 例如,如果对话框已关闭并通过此方法返回 @no__t 值0值,则您可以阻止对执行 @no__t 的代码执行。For example, if the dialog box was closed and returned the DialogResult.Cancel value through this method, you could prevent code following the call to ShowDialog from executing.

当窗体显示为模式对话框时,单击 "关闭" 按钮(窗体右上角带有 X 的按钮)将导致隐藏窗体,并将 DialogResult 属性设置为 DialogResult.CancelWhen a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. 与非模式窗体不同,当用户单击对话框的 "关闭窗体" 按钮或设置 @no__t 属性的值时,.NET Framework 不会调用 Close 方法。Unlike non-modal forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. 改为隐藏窗体,而无需创建新的对话框实例即可显示该窗体。Instead the form is hidden and can be shown again without creating a new instance of the dialog box. 由于作为对话框显示的窗体是隐藏的,而不是关闭的,因此,当您的应用程序不再需要窗体时,您必须调用窗体的 @no__t 0 方法。Because a form displayed as a dialog box is hidden instead of closed, you must call the Dispose method of the form when the form is no longer needed by your application.

此版本的 ShowDialog 方法不将窗体或控件指定为其所有者。This version of the ShowDialog method does not specify a form or control as its owner. 调用此版本时,当前活动窗口将成为该对话框的所有者。When this version is called, the currently active window is made the owner of the dialog box. 如果要指定特定的所有者,请使用此方法的其他版本。If you want to specify a specific owner, use the other version of this method.

另请参阅

ShowDialog(IWin32Window) ShowDialog(IWin32Window) ShowDialog(IWin32Window) ShowDialog(IWin32Window)

将窗体显示为具有指定所有者的模式对话框。Shows the form as a modal dialog box with the specified owner.

public:
 System::Windows::Forms::DialogResult ShowDialog(System::Windows::Forms::IWin32Window ^ owner);
public System.Windows.Forms.DialogResult ShowDialog (System.Windows.Forms.IWin32Window owner);
member this.ShowDialog : System.Windows.Forms.IWin32Window -> System.Windows.Forms.DialogResult
Public Function ShowDialog (owner As IWin32Window) As DialogResult

参数

owner
IWin32Window IWin32Window IWin32Window IWin32Window

任何实现 IWin32Window(表示将拥有模式对话框的顶级窗口)的对象。Any object that implements IWin32Window that represents the top-level window that will own the modal dialog box.

返回

异常

owner 参数中指定的窗体就是显示的窗体。The form specified in the owner parameter is the same as the form being shown.

要显示的窗体已经可见。The form being shown is already visible.

- 或 --or- 所显示窗体被禁用。The form being shown is disabled.

-or- 显示的窗体不是顶级窗口。The form being shown is not a top-level window.

-or- 显示为对话框的窗体已经是模式窗体。The form being shown as a dialog box is already a modal form.

-or- 当前进程不是以用户交互模式运行的(有关更多信息,请参见 UserInteractive)。The current process is not running in user interactive mode (for more information, see UserInteractive).

示例

下面的示例将窗体显示为模式对话框,并在确定是否在对话框窗体上读取 TextBox 控件的值之前计算对话框的返回值。The following example displays a form as a modal dialog box and evaluates the return value of the dialog box before determining whether to read the value of a TextBox control on the dialog box form. 此示例要求创建一个名为 @no__t 的 @no__t 0,其中包含一个名为 TextBox 的 @no__t 控件。This example requires that a Form named Form2 is created and that it contains a TextBox control named TextBox1. 该示例使用为对话框指定所有者的 @no__t 的版本。The example uses the version of ShowDialog that specifies an owner for the dialog box.

void ShowMyDialogBox()
{
   Form2^ testDialog = gcnew Form2;
   
   // Show testDialog as a modal dialog and determine if DialogResult = OK.
   if ( testDialog->ShowDialog( this ) == ::DialogResult::OK )
   {
      
      // Read the contents of testDialog's TextBox.
      this->txtResult->Text = testDialog->TextBox1->Text;
   }
   else
   {
      this->txtResult->Text = "Cancelled";
   }

   delete testDialog;
}
public void ShowMyDialogBox()
{
   Form2 testDialog = new Form2();

   // Show testDialog as a modal dialog and determine if DialogResult = OK.
   if (testDialog.ShowDialog(this) == DialogResult.OK)
   {
      // Read the contents of testDialog's TextBox.
      this.txtResult.Text = testDialog.TextBox1.Text;
   }
   else
   {
      this.txtResult.Text = "Cancelled";
   }
   testDialog.Dispose();
}
Public Sub ShowMyDialogBox()
    Dim testDialog As New Form2()
    
    ' Show testDialog as a modal dialog and determine if DialogResult = OK.
    If testDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
        ' Read the contents of testDialog's TextBox.
        txtResult.Text = testDialog.TextBox1.Text
    Else
        txtResult.Text = "Cancelled"
    End If
    testDialog.Dispose()
End Sub

注解

您可以使用此方法在应用程序中显示模式对话框。You can use this method to display a modal dialog box in your application. 如果调用此方法,则在对话框关闭后才会执行它后面的代码。When this method is called, the code following it is not executed until after the dialog box is closed. 可以通过将 @no__t 值分配给窗体上 Button 的 @no__t 属性,或在代码中设置窗体的 DialogResult 属性,来为该对话框分配一个值。The dialog box can be assigned one of the values of DialogResult by assigning it to the DialogResult property of a Button on the form or by setting the DialogResult property of the form in code. 此方法随后将返回此值。This value is then returned by this method. 您可以使用此返回值确定如何处理对话框中发生的操作。You can use this return value to determine how to process the actions that occurred in the dialog box. 例如,如果对话框已关闭并通过此方法返回 @no__t 值0值,则您可以阻止对执行 @no__t 的代码执行。For example, if the dialog box was closed and returned the DialogResult.Cancel value through this method, you could prevent code following the call to ShowDialog from executing.

当窗体显示为模式对话框时,单击 "关闭" 按钮(窗体右上角带有 X 的按钮)将导致隐藏窗体,并将 DialogResult 属性设置为 DialogResult.CancelWhen a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. 与无模式窗体不同,当用户单击对话框的关闭窗体按钮或设置 @no__t 属性的值时,.NET Framework 不会调用 Close 方法。Unlike modeless forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. 改为隐藏窗体,而无需创建新的对话框实例即可显示该窗体。Instead the form is hidden and can be shown again without creating a new instance of the dialog box. 由于作为对话框显示的窗体是隐藏的,而不是关闭的,因此,当您的应用程序不再需要窗体时,您必须调用窗体的 @no__t 0 方法。Because a form displayed as a dialog box is hidden instead of closed, you must call the Dispose method of the form when the form is no longer needed by your application.

此版本的 ShowDialog 方法允许您指定特定的窗体,该窗体将拥有显示的对话框。This version of the ShowDialog method allows you to specify a specific form that will own the dialog box that is shown.

另请参阅

适用于