Share via


方法 : ダイアログ ボックスの結果を取得する

ダイアログ ボックスを閉じた後、ダイアログ ボックスが表示されていたフォームでは、閉じた後の結果を取得できます。結果を取得するには、そのダイアログ ボックスの DialogResult プロパティ、または ShowDialog メソッドの戻り値を参照します。 ダイアログ ボックスが表示されていたフォームは、戻り値に従って応答します。

DialogResult 値を取得するには

  • ダイアログ ボックスを表示するメソッドに、次のようなコードを追加します。

    通常は、ダイアログ ボックスを作成および表示するコードの後に挿入します。

    Public Sub DisplayDialog()
       ' Create and display an instance of the dialog box.
       Dim dlg as New Form()
    
       ' Show the dialog and determine the state of the 
       ' DialogResult property for the form.
       If dlg.ShowDialog = DialogResult.OK Then
          ' Do something here to handle data from dialog box.
       End If
    End Sub
    
    private void DisplayDialog() 
    {
       // Create and display an instance of the dialog box
       Form dlg = new Form();
    
       // Show the dialog and determine the state of the 
       // DialogResult property for the form.
       if (dlg.ShowDialog() == DialogResult.OK ) 
       {
          // Do something here to handle data from dialog box.
       }
    }
    
    private:
       void DisplayDialog()
       {
          // Create and display an instance of the dialog box
          Form^ dlg = gcnew Form();
    
          // Show the dialog and determine the state of the 
          // DialogResult property for the form.
          if (dlg->ShowDialog() == DialogResult::OK )
          {
             // Do something here to handle data from dialog box.
          }
       }
    

    注意

    ダイアログ ボックスを適切に破棄するためには、フォームに対して Dispose メソッドを呼び出すことが重要です。これは、Close ボックスをクリックしたり、Close メソッドを呼び出したりしても自動的には実行されません。

参照

処理手順

方法 : デザイン時にダイアログ ボックスを作成する

方法 : ダイアログ ボックスを閉じて、ユーザー入力を保持する

概念

ダイアログ ボックスへのユーザー入力

その他の技術情報

Windows フォームのダイアログ ボックス

新しい Windows フォームの作成