다음을 통해 공유


Form.DialogResult 속성

폼의 대화 상자 결과를 가져오거나 설정합니다.

네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)

구문

‘선언
Public Property DialogResult As DialogResult
‘사용 방법
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)

속성 값

폼이 대화 상자로 사용되는 경우 해당 폼의 결과를 나타내는 DialogResult입니다.

예외

예외 형식 조건

InvalidEnumArgumentException

지정된 값이 유효한 값 범위를 벗어난 경우

설명

폼의 대화 상자 결과는 폼이 모달 대화 상자로 표시될 때 해당 폼에서 반환된 값입니다. 폼이 대화 상자로 표시되는 경우 이 속성을 DialogResult 열거형의 값으로 설정하면 해당 폼의 대화 상자 결과 값이 설정되고, 모달 대화 상자가 숨겨지며, 컨트롤이 호출 폼으로 반환됩니다. 일반적으로 이 속성은 폼에 있는 Button 컨트롤의 DialogResult 속성에 의해 설정됩니다. Button 컨트롤을 클릭하면 ButtonDialogResult 속성에 할당된 값이 폼의 DialogResult 속성에 할당됩니다.

폼이 모달 대화 상자로 표시되는 경우 닫기 단추(폼의 오른쪽 위 모퉁이에 X가 붙은 단추)를 클릭하면 해당 폼이 숨겨지고 DialogResult 속성이 DialogResult.Cancel로 설정됩니다. Close 메서드는 사용자가 대화 상자의 닫기 단추를 클릭하거나 DialogResult 속성 값을 설정할 때 자동으로 호출되지 않습니다. 대신 폼이 숨겨지며 대화 상자의 새 인스턴스를 만들지 않고 숨겨진 폼이 다시 표시될 수 있습니다. 따라서 폼이 응용 프로그램에 더 이상 필요하지 않은 경우에는 해당 폼의 Dispose 메서드를 호출해야 합니다.

이 속성을 사용하면 대화 상자에서 수행된 작업을 제대로 처리할 수 있도록 대화 상자가 닫히는 방식을 결정할 수 있습니다.

참고

폼의 Closing 이벤트에 대한 이벤트 처리기에서 DialogResult 속성을 설정하면 사용자가 닫기 단추를 클릭할 때 DialogResult 속성에 할당된 값을 재정의할 수 있습니다.

참고

Form이 모덜리스 창으로 표시되는 경우 폼이 닫힐 때 해당 폼의 리소스가 자동으로 해제되므로 DialogResult 속성에서 반환된 값이 폼에 할당된 값을 반환하지 않을 수 있습니다.

예제

다음 코드 예제에서는 폼이 대화 상자로 표시되고 폼의 DialogResult 속성을 참조하여 폼에 있는 확인 또는 취소 단추가 클릭되었는지 여부를 나타내는 메시지 상자가 표시됩니다.

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

플랫폼

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

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

Form 클래스
Form 멤버
System.Windows.Forms 네임스페이스
DialogResult 열거형
Button.DialogResult 속성