Comment : récupérer le résultat des boîtes de dialogue

Une fois qu'une boîte de dialogue est fermée, le formulaire qui l'affichait peut récupérer le résultat de celle-ci en référençant sa propriété DialogResult ou la valeur de retour d'un appel à la méthode ShowDialog. Le formulaire qui a affiché la boîte de dialogue répond alors en fonction de la valeur retournée.

Pour récupérer la valeur de la propriété DialogResult

  • Ajoutez le code suivant à la méthode qui a affiché la boîte de dialogue.

    En général, ce code est placé après celui qui a servi à créer et afficher la boîte de dialogue :

    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 = 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.
          }
       }
    

    Notes

    Il est extrêmement important d'appeler la méthode Dispose sur le formulaire pour supprimer correctement la boîte de dialogue. Rappelez-vous que cela ne s'effectue pas automatiquement en cliquant sur la zone Fermer ou en appelant la méthode Close.

Voir aussi

Tâches

Comment : créer des boîtes de dialogue au moment du design
Comment : fermer les boîtes de dialogue et conserver les données entrées par l'utilisateur

Concepts

Données entrées par l'utilisateur dans les boîtes de dialogue

Autres ressources

Boîtes de dialogue dans les Windows Forms
Création d'un nouveau Windows Form