Form.DialogResult Propriedade

Definição

Obtém ou define o resultado da caixa de diálogo para o formulário.

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

Valor da propriedade

Um DialogResult que representa o resultado do formulário quando usado como uma caixa de diálogo.

Atributos

Exceções

O valor especificado está fora do intervalo de valores válidos.

Exemplos

O exemplo a seguir exibe um formulário como uma caixa de diálogo e exibe uma caixa de mensagem indicando se o botão OK ou Cancelar do formulário foi clicado fazendo referência à DialogResult propriedade do formulário.

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

Comentários

O resultado da caixa de diálogo de um formulário é o valor retornado do formulário quando ele é exibido como uma caixa de diálogo modal. Se o formulário for exibido como uma caixa de diálogo, definir essa propriedade com um valor da DialogResult enumeração definirá o valor do resultado da caixa de diálogo para o formulário, ocultará a caixa de diálogo modal e retornará o controle para o formulário de chamada. Normalmente, essa propriedade é definida pela DialogResult propriedade de um Button controle no formulário. Quando o usuário clica no Button controle, o valor atribuído à DialogResult propriedade do Button é atribuído à DialogResult propriedade do formulário.

Quando um formulário é exibido como uma caixa de diálogo modal, clicar no botão Fechar (o botão com um X no canto superior direito do formulário) faz com que o formulário fique oculto e a DialogResult propriedade seja definida DialogResult.Cancelcomo . O Close método não é chamado automaticamente quando o usuário clica no botão Fechar de uma caixa de diálogo ou define o valor da DialogResult propriedade. Em vez disso, o formulário está oculto e pode ser mostrado novamente sem criar uma nova instância da caixa de diálogo. Devido a esse comportamento, você deve chamar o Dispose método do formulário quando o formulário não for mais necessário para seu aplicativo.

Você pode usar essa propriedade para determinar como uma caixa de diálogo é fechada para processar corretamente as ações executadas na caixa de diálogo.

Observação

Você pode substituir o valor atribuído à DialogResult propriedade quando o usuário clica no botão Fechar definindo a DialogResult propriedade em um manipulador de eventos para o Closing evento do formulário.

Observação

Se um Form for exibido como uma janela de modelagem, o valor retornado pela DialogResult propriedade poderá não retornar um valor atribuído ao formulário porque os recursos do formulário são liberados automaticamente quando o formulário é fechado.

Aplica-se a

Confira também