Form.DialogResult Właściwość

Definicja

Pobiera lub ustawia wynik okna dialogowego formularza.

public:
 property System::Windows::Forms::DialogResult DialogResult { System::Windows::Forms::DialogResult get(); void set(System::Windows::Forms::DialogResult value); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.DialogResult DialogResult { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.DialogResult : System.Windows.Forms.DialogResult with get, set
Public Property DialogResult As DialogResult

Wartość właściwości

Obiekt DialogResult reprezentujący wynik formularza, który jest używany jako okno dialogowe.

Atrybuty

Wyjątki

Określona wartość jest poza zakresem prawidłowych wartości.

Przykłady

Poniższy przykład wyświetla formularz jako okno dialogowe i wyświetla okno komunikatu wskazujące, czy przycisk OK lub Anuluj formularza został kliknięty przez odwołanie do DialogResult właściwości formularza.

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.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();
    }
 }
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

Uwagi

Wynikiem okna dialogowego formularza jest wartość zwracana z formularza, gdy jest wyświetlana jako modalne okno dialogowe. Jeśli formularz jest wyświetlany jako okno dialogowe, ustawienie tej właściwości z wartością z DialogResult wyliczenia ustawia wartość wyniku okna dialogowego formularza, ukrywa modalne okno dialogowe i zwraca kontrolkę do formularza wywołującego. Ta właściwość jest zwykle ustawiana przez DialogResult właściwość kontrolki Button w formularzu. Gdy użytkownik kliknie kontrolkę Button , wartość przypisana do DialogResult właściwości obiektu Button zostanie przypisana do DialogResult właściwości formularza.

Gdy formularz jest wyświetlany jako modalne okno dialogowe, kliknięcie przycisku Zamknij (przycisk z symbolem X w prawym górnym rogu formularza) powoduje ukrycie formularza i DialogResult ustawienie właściwości na DialogResult.Cancelwartość . Metoda Close nie jest wywoływana automatycznie, gdy użytkownik kliknie przycisk Zamknij okna dialogowego lub ustawi wartość DialogResult właściwości. Zamiast tego formularz jest ukryty i można go wyświetlić ponownie bez tworzenia nowego wystąpienia okna dialogowego. Ze względu na to zachowanie należy wywołać Dispose metodę formularza, gdy formularz nie jest już potrzebny przez aplikację.

Za pomocą tej właściwości można określić sposób zamknięcia okna dialogowego w celu prawidłowego przetwarzania akcji wykonywanych w oknie dialogowym.

Uwaga

Wartość przypisaną DialogResult do właściwości można zastąpić, gdy użytkownik kliknie przycisk Zamknij , ustawiając DialogResult właściwość w programie obsługi zdarzeń dla Closing zdarzenia formularza.

Uwaga

Jeśli element Form jest wyświetlany jako okno bez moderowania, wartość zwrócona przez DialogResult właściwość może nie zwracać wartości przypisanej do formularza, ponieważ zasoby formularza są automatycznie zwalniane po zamknięciu formularza.

Dotyczy

Zobacz też