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

範例

下列範例會將表單顯示為強制回應對話方塊,並在決定是否要讀取對話方塊表單上 @no__t 0 控制項的值之前,先評估對話方塊的傳回值。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. 這個範例需要建立名為 testDialogForm,而且它包含名為 TextBox1 的 @no__t 2 控制項。This example requires that a Form named testDialog is created and that it contains a TextBox control named TextBox1. 此外,此範例需要包含此範例中的程式碼,並從不同的 Form 呼叫,才能以強制回應對話方塊的形式顯示 testDialogFurthermore, 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

備註

您可以使用這個方法,在您的應用程式中顯示模式對話方塊。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 0 列舉的其中一個值指派給表單上 ButtonDialogResult 屬性,或在程式碼中設定表單的 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 值,您可能會在呼叫之後防止程式碼執行 ShowDialogFor 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.Cancel]。When 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. 不同于非強制回應表單,當使用者按一下對話方塊的 [關閉表單] 按鈕或設定 DialogResult 屬性的值時,.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. 因為顯示為對話方塊的表單是隱藏的,而不是關閉的,所以當您的應用程式不再需要表單時,您必須呼叫表單的 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).

範例

下列範例會將表單顯示為強制回應對話方塊,並在決定是否要讀取對話方塊表單上 @no__t 0 控制項的值之前,先評估對話方塊的傳回值。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. 這個範例需要建立名為 Form2Form,而且它包含名為 TextBox1 的 @no__t 2 控制項。This 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

備註

您可以使用這個方法,在您的應用程式中顯示模式對話方塊。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 的其中一個值指派給此對話方塊,方法是將它指派給表單上 @no__t 2 的 DialogResult 屬性,或在程式碼中設定表單的 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 值,您可能會在呼叫之後防止程式碼執行 ShowDialogFor 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.Cancel]。When 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. 不同于非模式表單,當使用者按一下對話方塊的 [關閉表單] 按鈕或設定 DialogResult 屬性的值時,.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. 因為顯示為對話方塊的表單是隱藏的,而不是關閉的,所以當您的應用程式不再需要表單時,您必須呼叫表單的 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.

另請參閱

適用於