Gewusst wie: Abrufen des Ergebnisses für Dialogfelder

Nach dem Schließen eines Dialogfelds kann im Formular, aus dem das Dialogfeld geöffnet wurde, auf das Ergebnis des Dialogfelds zugegriffen werden, indem entweder auf dessen DialogResult-Eigenschaft oder auf den Rückgabewert eines Aufrufs der ShowDialog-Methode verwiesen wird. Anhand des zurückgegebenen Werts wird dann entschieden, wie das Formular reagiert, durch das das Dialogfeld angezeigt wurde.

So rufen Sie den DialogResult-Wert ab

  • Fügen Sie der Methode, durch die das Dialogfeld angezeigt wird, mit dem folgenden Beispiel vergleichbaren Code hinzu.

    Dieser Code wird normalerweise nach dem Code eingefügt, durch den das Dialogfeld erstellt und angezeigt wird:

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

    Hinweis

    Beachten Sie, dass die Dispose-Methode für das Formular aufgerufen werden muss, damit das Dialogfeld ordnungsgemäß gelöscht wird.Denken Sie daran, dass dies nicht automatisch beim Klicken auf die Schaltfläche Schließen oder beim Aufruf der Close-Methode erfolgt.

Siehe auch

Aufgaben

Gewusst wie: Erstellen von Dialogfeldern zur Entwurfszeit

Gewusst wie: Schließen von Dialogfeldern und Speichern von Benutzereingaben

Konzepte

Benutzereingaben in Dialogfelder

Weitere Ressourcen

Dialogfelder in Windows Forms

Erstellen neuer Windows Forms