Form.DialogResult Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém ou define o resultado do 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 de Propriedade
A DialogResult que representa o resultado do formulário quando usado como caixa de diálogo.
- Atributos
Exceções
O valor especificado está fora do intervalo de valores válidos.
Exemplos
O exemplo seguinte mostra um formulário como uma caixa de diálogo e uma caixa de mensagem que indica se o botão OK ou Cancelar do formulário foi clicado ao referenciar a 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
Observações
O resultado de diálogo de um formulário é o valor que é devolvido do formulário quando este é exibido como uma caixa de diálogo modal. Se o formulário for exibido como uma caixa de diálogo, definir esta propriedade com um valor da DialogResult enumeração define o valor do resultado da caixa de diálogo para o formulário, oculta a caixa de diálogo modal e devolve o controlo ao formulário que chama. Esta propriedade é tipicamente definida pela DialogResult propriedade de um Button controlo na forma. Quando o utilizador clica no Button controlo, o valor atribuído à DialogResult propriedade de é 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 para DialogResult.Cancel. O Close método não é chamado automaticamente quando o utilizador clica no botão Fechar de uma caixa de diálogo ou define o valor da DialogResult propriedade. Em vez disso, o formulário é oculto e pode ser mostrado novamente sem criar uma nova instância da caixa de diálogo. Por causa deste comportamento, deve chamar o Dispose método do formulário quando o formulário já não for necessário para a sua candidatura.
Pode usar esta propriedade para determinar como uma caixa de diálogo está fechada, de modo a processar corretamente as ações realizadas na caixa de diálogo.
Note
Pode sobrescrever o valor atribuído à DialogResult propriedade quando o utilizador clica no botão Fechar , definindo a DialogResult propriedade num gestor de eventos para o Closing evento do formulário.
Note
Se a Form for exibido como uma janela sem modos, o valor devolvido pela DialogResult propriedade pode não devolver um valor atribuído ao formulário porque os recursos do formulário são automaticamente libertados quando o formulário é fechado.