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 code 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. 此示例需要Form名为testDialog创建和它所包含TextBox控件命名为TextBox1This example requires that a Form named testDialog is created and that it contains a TextBox control named TextBox1. 此外,该示例需要在此示例中的代码是包含和名为来自不同Form以便显示testDialog为模式对话框。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. 此示例使用的版本ShowDialog指定所有者为对话框的。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 'ShowMyDialogBox

注解

此方法可用于在应用程序中显示模式对话框。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通过将其分配给枚举DialogResult的属性Button窗体上或通过设置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. 例如,如果对话框已关闭,并且返回DialogResult.Cancel值通过此方法中,你可以阻止之后到调用代码ShowDialog执行。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. 与非模式窗体不同Close当用户单击关闭窗体按钮的对话框中,或设置的值不由.NET Framework 调用方法DialogResult属性。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. 因为窗体显示为对话框中隐藏的而不是已关闭,则必须调用Dispose窗体时应用程序不再需要在窗体的方法。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 code 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. 此示例需要Form名为Form2创建和它所包含TextBox控件命名为TextBox1This example requires that a Form named Form2 is created and that it contains a TextBox control named TextBox1. 此示例使用的版本ShowDialog指定所有者为对话框的。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 'ShowMyDialogBox

注解

此方法可用于在应用程序中显示模式对话框。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通过将其分配给DialogResult的属性Button窗体上或通过设置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. 例如,如果对话框已关闭,并且返回DialogResult.Cancel值通过此方法中,你可以阻止之后到调用代码ShowDialog执行。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. 与无模式窗体不同Close当用户单击关闭窗体按钮的对话框中,或设置的值不由.NET Framework 调用方法DialogResult属性。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. 因为窗体显示为对话框中隐藏的而不是已关闭,则必须调用Dispose窗体时应用程序不再需要在窗体的方法。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.

另请参阅

适用于