Freigeben über


Form.DialogResult-Eigenschaft

Ruft das Dialogergebnis für das Formular ab oder legt dieses fest.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Property DialogResult As DialogResult
'Usage
Dim instance As Form
Dim value As DialogResult

value = instance.DialogResult

instance.DialogResult = value
public DialogResult DialogResult { get; set; }
public:
property DialogResult DialogResult {
    DialogResult get ();
    void set (DialogResult value);
}
/** @property */
public DialogResult get_DialogResult ()

/** @property */
public void set_DialogResult (DialogResult value)
public function get DialogResult () : DialogResult

public function set DialogResult (value : DialogResult)

Eigenschaftenwert

Ein DialogResult, das das Ergebnis des Formulars bei Verwendung als Dialogfeld darstellt.

Ausnahmen

Ausnahmetyp Bedingung

InvalidEnumArgumentException

Der angegebene Wert liegt außerhalb des gültigen Wertebereichs.

Hinweise

Das Dialogergebnis eines Formulars ist der Wert, der vom Formular zurückgegeben wird, wenn es als modales Dialogfeld angezeigt wird. Bei einem als Dialogfeld angezeigten Formular wird durch Festlegen dieser Eigenschaft auf einen Wert der DialogResult-Enumeration der Wert des Dialogergebnisses für das Formular festgelegt. Außerdem wird das modale Dialogfeld ausgeblendet, und die Steuerung wird an das aufrufende Formular übergeben. Diese Eigenschaft wird i. d. R. durch die DialogResult-Eigenschaft eines Button-Steuerelements im Formular festgelegt. Wenn der Benutzer auf das Button-Steuerelement klickt, wird der der DialogResult-Eigenschaft von Button zugewiesene Wert der DialogResult-Eigenschaft des Formulars zugewiesen.

Bei einem als modales Dialogfeld angezeigten Formular wird durch Klicken auf die Schaltfläche Schließen (die Schaltfläche mit einem X in der rechten oberen Ecke des Formulars) das Formular ausgeblendet, und die DialogResult-Eigenschaft wird auf DialogResult.Cancel festgelegt. Die Close-Methode wird nicht automatisch aufgerufen, wenn der Benutzer auf die Schaltfläche Schließen eines Dialogfelds klickt oder den Wert der DialogResult-Eigenschaft festlegt. Stattdessen wird das Formular ausgeblendet, und es kann ohne Erstellen einer neuen Instanz des Dialogfelds wieder eingeblendet werden. Aufgrund dieses Verhaltens müssen Sie die Dispose-Methode des Formulars aufrufen, wenn das Formular von der Anwendung nicht mehr benötigt wird.

Mit dieser Eigenschaft können Sie bestimmen, wie ein Dialogfeld geschlossen wird, um die im Dialogfeld durchgeführten Vorgänge ordnungsgemäß verarbeiten zu können.

Hinweis

Sie können den Wert überschreiben, der der DialogResult-Eigenschaft beim Klicken auf die Schaltfläche Schließen zugewiesen wird, indem Sie die DialogResult-Eigenschaft in einem Ereignishandler für das Closing-Ereignis des Formulars festlegen.

Hinweis

Bei einer als nicht modales Fenster angezeigten Form gibt der von der DialogResult-Eigenschaft zurückgegebene Wert möglicherweise keinen dem Formular zugewiesenen Wert zurück, da die Ressourcen des Formulars automatisch freigegeben werden, sobald das Formular geschlossen wird.

Beispiel

Im folgenden Codebeispiel wird ein Formular als Dialogfeld angezeigt. Anschließend wird ein Meldungsfeld angezeigt, in dem über einen Verweis auf die DialogResult-Eigenschaft des Formulars angegeben wird, ob auf die Schaltfläche OK oder Abbrechen des Formulars geklickt wurde.

Public Sub CreateMyForm()
    ' Create a new instance of the form.
    Dim form1 As New Form()
    ' Create two buttons to use as the accept and cancel buttons.
    Dim button1 As New Button()
    Dim button2 As New Button()
    
    ' Set the text of button1 to "OK".
    button1.Text = "OK"
    ' Set the position of the button on the form.
    button1.Location = New Point(10, 10)
    ' Set the text of button2 to "Cancel".
    button2.Text = "Cancel"
    ' Set the position of the button based on the location of button1.
    button2.Location = New Point(button1.Left, button1.Height + button1.Top + 10)
    ' Make button1's dialog result OK.
    button1.DialogResult = DialogResult.OK
    ' Make button2's dialog result Cancel.
    button2.DialogResult = DialogResult.Cancel
    ' Set the caption bar text of the form.   
    form1.Text = "My Dialog Box"
    
    ' Define the border style of the form to a dialog box.
    form1.FormBorderStyle = FormBorderStyle.FixedDialog
    ' Set the accept button of the form to button1.
    form1.AcceptButton = button1
    ' Set the cancel button of the form to button2.
    form1.CancelButton = button2
    ' Set the start position of the form to the center of the screen.
    form1.StartPosition = FormStartPosition.CenterScreen
    
    ' Add button1 to the form.
    form1.Controls.Add(button1)
    ' Add button2 to the form.
    form1.Controls.Add(button2)
    
    ' Display the form as a modal dialog box.
    form1.ShowDialog()
    
    ' Determine if the OK button was clicked on the dialog box.
    If form1.DialogResult = DialogResult.OK Then
        ' Display a message box indicating that the OK button was clicked.
        MessageBox.Show("The OK button on the form was clicked.")
        ' Optional: Call the Dispose method when you are finished with the dialog box.
        form1.Dispose
    ' Display a message box indicating that the Cancel button was clicked.
    Else
        MessageBox.Show("The Cancel button on the form was clicked.")
        ' Optional: Call the Dispose method when you are finished with the dialog box.
        form1.Dispose
    End If
End Sub 'CreateMyForm 
public void CreateMyForm()
 {
    // Create a new instance of the form.
    Form form1 = new Form();
    // Create two buttons to use as the accept and cancel buttons.
    Button button1 = new Button ();
    Button button2 = new Button ();
   
    // Set the text of button1 to "OK".
    button1.Text = "OK";
    // Set the position of the button on the form.
    button1.Location = new Point (10, 10);
    // Set the text of button2 to "Cancel".
    button2.Text = "Cancel";
    // Set the position of the button based on the location of button1.
    button2.Location 
       = new Point (button1.Left, button1.Height + button1.Top + 10);
    // Make button1's dialog result OK.
    button1.DialogResult = DialogResult.OK;
    // Make button2's dialog result Cancel.
    button2.DialogResult = DialogResult.Cancel;
    // Set the caption bar text of the form.   
    form1.Text = "My Dialog Box";
 
    // Define the border style of the form to a dialog box.
    form1.FormBorderStyle = FormBorderStyle.FixedDialog;
    // Set the accept button of the form to button1.
    form1.AcceptButton = button1;
    // Set the cancel button of the form to button2.
    form1.CancelButton = button2;
    // Set the start position of the form to the center of the screen.
    form1.StartPosition = FormStartPosition.CenterScreen;
    
    // Add button1 to the form.
    form1.Controls.Add(button1);
    // Add button2 to the form.
    form1.Controls.Add(button2);
    
    // Display the form as a modal dialog box.
    form1.ShowDialog();
 
    // Determine if the OK button was clicked on the dialog box.
    if (form1.DialogResult == DialogResult.OK)
    {
       // Display a message box indicating that the OK button was clicked.
       MessageBox.Show("The OK button on the form was clicked.");
       // Optional: Call the Dispose method when you are finished with the dialog box.
       form1.Dispose();
    }
    else
    {
       // Display a message box indicating that the Cancel button was clicked.
       MessageBox.Show("The Cancel button on the form was clicked.");
       // Optional: Call the Dispose method when you are finished with the dialog box.
       form1.Dispose();
    }
 }
    
void CreateMyForm()
{
   
   // Create a new instance of the form.
   Form^ form1 = gcnew Form;
   
   // Create two buttons to use as the accept and cancel buttons.
   Button^ button1 = gcnew Button;
   Button^ button2 = gcnew Button;
   
   // Set the text of button1 to "OK".
   button1->Text = "OK";
   
   // Set the position of the button on the form.
   button1->Location = Point(10,10);
   
   // Set the text of button2 to "Cancel".
   button2->Text = "Cancel";
   
   // Set the position of the button based on the location of button1.
   button2->Location = Point(button1->Left,button1->Height + button1->Top + 10);
   
   // Make button1's dialog result OK.
   button1->DialogResult = ::DialogResult::OK;
   
   // Make button2's dialog result Cancel.
   button2->DialogResult = ::DialogResult::Cancel;
   
   // Set the caption bar text of the form.   
   form1->Text = "My Dialog Box";
   
   // Define the border style of the form to a dialog box.
   form1->FormBorderStyle = ::FormBorderStyle::FixedDialog;
   
   // Set the accept button of the form to button1.
   form1->AcceptButton = button1;
   
   // Set the cancel button of the form to button2.
   form1->CancelButton = button2;
   
   // Set the start position of the form to the center of the screen.
   form1->StartPosition = FormStartPosition::CenterScreen;
   
   // Add button1 to the form.
   form1->Controls->Add( button1 );
   
   // Add button2 to the form.
   form1->Controls->Add( button2 );
   
   // Display the form as a modal dialog box.
   form1->ShowDialog();
   
   // Determine if the OK button was clicked on the dialog box.
   if ( form1->DialogResult == ::DialogResult::OK )
   {
      
      // Display a message box indicating that the OK button was clicked.
      MessageBox::Show( "The OK button on the form was clicked." );
      
      // Optional: Call the Dispose method when you are finished with the dialog box.
      delete form1;
   }
   else
   {
      
      // Display a message box indicating that the Cancel button was clicked.
      MessageBox::Show( "The Cancel button on the form was clicked." );
      
      // Optional: Call the Dispose method when you are finished with the dialog box.
      delete form1;
   }
}
public void CreateMyForm()
{
    // Create a new instance of the form.
    Form form1 = new Form();

    // Create two buttons to use as the accept and cancel buttons.
    Button button1 = new Button();
    Button button2 = new Button();

    // Set the text of button1 to "OK".
    button1.set_Text("OK");

    // Set the position of the button on the form.
    button1.set_Location(new Point(10, 10));

    // Set the text of button2 to "Cancel".
    button2.set_Text("Cancel");

    // Set the position of the button based on the location of button1.
    button2.set_Location(new Point(button1.get_Left(), 
        button1.get_Height() + button1.get_Top() + 10));

    // Make button1's dialog result OK.
    button1.set_DialogResult(get_DialogResult().OK);

    // Make button2's dialog result Cancel.
    button2.set_DialogResult(get_DialogResult().Cancel);

    // Set the caption bar text of the form.   
    form1.set_Text("My Dialog Box");

    // Define the border style of the form to a dialog box.
    form1.set_FormBorderStyle(get_FormBorderStyle().FixedDialog);

    // Set the accept button of the form to button1.
    form1.set_AcceptButton(button1);

    // Set the cancel button of the form to button2.
    form1.set_CancelButton(button2);

    // Set the start position of the form to the center of the screen.
    form1.set_StartPosition(FormStartPosition.CenterScreen);

    // Add button1 to the form.
    form1.get_Controls().Add(button1);

    // Add button2 to the form.
    form1.get_Controls().Add(button2);

    // Display the form as a modal dialog box.
    form1.ShowDialog();

    // Determine if the OK button was clicked on the dialog box.
    if (form1.get_DialogResult().Equals(get_DialogResult().OK)) {
        // Display a message box indicating that the OK button was clicked.
        MessageBox.Show("The OK button on the form was clicked.");

        // Optional: Call the Dispose method when you are finished with 
        // the dialog box.
        form1.Dispose();
    }
    else {
        // Display a message box indicating that the Cancel button was 
        // clicked.
        MessageBox.Show("The Cancel button on the form was clicked.");

        // Optional: Call the Dispose method when you are finished with 
        // the dialog box.
        form1.Dispose();
    }
} //CreateMyForm

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Form-Klasse
Form-Member
System.Windows.Forms-Namespace
DialogResult-Enumeration
Button.DialogResult-Eigenschaft