다음을 통해 공유


Form.CancelButton 속성

사용자가 Enter 키를 누를 때 클릭되는 Button 컨트롤을 가져오거나 설정합니다.

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

구문

‘선언
Public Property CancelButton As IButtonControl
‘사용 방법
Dim instance As Form
Dim value As IButtonControl

value = instance.CancelButton

instance.CancelButton = value
public IButtonControl CancelButton { get; set; }
public:
property IButtonControl^ CancelButton {
    IButtonControl^ get ();
    void set (IButtonControl^ value);
}
/** @property */
public IButtonControl get_CancelButton ()

/** @property */
public void set_CancelButton (IButtonControl value)
public function get CancelButton () : IButtonControl

public function set CancelButton (value : IButtonControl)

속성 값

폼의 취소 단추를 나타내는 IButtonControl입니다.

설명

폼의 취소 단추는 사용자가 Esc 키를 누를 때마다 클릭되는 Button 컨트롤입니다. 이 속성에 할당된 단추는 현재 폼에 있거나 현재 폼의 컨테이너 내에 있는 IButtonControl이어야 합니다.

이 속성을 사용하면 사용자가 응용 프로그램에서 Esc 키를 누를 때 발생하는 기본 동작을 지정할 수 있습니다. 이 속성을 사용하면 마우스로 취소 단추를 직접 클릭하는 대신 변경 내용을 적용하지 않고 Esc 키를 눌러 창을 닫기만 하면 되므로 간단한 폼을 빠르게 탐색할 수 있습니다.

폼의 다른 컨트롤에서 Esc 키를 가로채면 CancelButton이 작동하지 않을 수 있습니다. 예를 들어, ComboBox가 폼에 열려 있는 경우 Esc 키를 누르면 폼이 닫히는 대신 ComboBox가 닫힙니다.

CancelButton에 할당된 IButtonControl 개체가 폼에 표시되지 않으면 Esc 키를 눌러도 아무런 효과가 없습니다.

예제

다음 코드 예제에서는 Form의 새 인스턴스를 만들고 ShowDialog 메서드를 호출하여 해당 폼을 대화 상자로 표시합니다. 이 예제에서는 FormBorderStyle, AcceptButton, CancelButton, MinimizeBox, MaximizeBoxStartPosition 속성을 설정하여 폼의 모양 및 기능을 대화 상자로 변경합니다. 또한 폼의 Controls 컬렉션에 있는 Add 메서드를 사용하여 두 개의 Button 컨트롤을 추가합니다. 그런 다음 HelpButton 속성을 사용하여 대화 상자의 캡션 표시줄에 도움말 단추를 표시합니다.

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)
    ' Set the caption bar text of the form.   
    form1.Text = "My Dialog Box"
    ' Display a help button on the form.
    form1.HelpButton = True
       
    ' Define the border style of the form to a dialog box.
    form1.FormBorderStyle = FormBorderStyle.FixedDialog
    ' Set the MaximizeBox to false to remove the maximize box.
    form1.MaximizeBox = False
    ' Set the MinimizeBox to false to remove the minimize box.
    form1.MinimizeBox = False
    ' 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()
End Sub
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);
   // Set the caption bar text of the form.   
   form1.Text = "My Dialog Box";
   // Display a help button on the form.
   form1.HelpButton = true;

   // Define the border style of the form to a dialog box.
   form1.FormBorderStyle = FormBorderStyle.FixedDialog;
   // Set the MaximizeBox to false to remove the maximize box.
   form1.MaximizeBox = false;
   // Set the MinimizeBox to false to remove the minimize box.
   form1.MinimizeBox = false;
   // 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();
}
public:
   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 );
      // Set the caption bar text of the form.   
      form1->Text = "My Dialog Box";
      // Display a help button on the form.
      form1->HelpButton = true;
      
      // Define the border style of the form to a dialog box.
      form1->FormBorderStyle = ::FormBorderStyle::FixedDialog;
      // Set the MaximizeBox to false to remove the maximize box.
      form1->MaximizeBox = false;      
      // Set the MinimizeBox to false to remove the minimize box.
      form1->MinimizeBox = false;
      // 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();
   }
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));

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

    // Display a help button on the form.
    form1.set_HelpButton(true);

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

    // Set the MaximizeBox to false to remove the maximize box.
    form1.set_MaximizeBox(false);

    // Set the MinimizeBox to false to remove the minimize box.
    form1.set_MinimizeBox(false);

    // 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();
} //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에서 지원

참고 항목

참조

Form 클래스
Form 멤버
System.Windows.Forms 네임스페이스
Form.AcceptButton 속성
IButtonControl