다음을 통해 공유


방법: Windows Forms 응용 프로그램에서 이벤트 사용

업데이트: 2007년 11월

Windows Forms 응용 프로그램의 일반적인 시나리오는 컨트롤이 있는 폼을 표시한 다음 사용자가 클릭하는 컨트롤에 따라 특정 작업을 수행하는 것입니다. 예를 들어, Button 컨트롤은 사용자가 폼에서 해당 컨트롤을 클릭하면 이벤트를 발생시킵니다. 응용 프로그램은 이 이벤트를 처리하여 단추 클릭에 해당하는 적절한 응용 프로그램 논리를 수행할 수 있습니다.

Windows Forms에 대한 자세한 내용은 Windows Forms 시작을 참조하십시오.

Windows Form에서 단추 클릭 이벤트를 처리하려면

  1. Button 컨트롤이 있는 Windows Form을 만듭니다.

    private Button button;
    
    Private WithEvents myButton As Button
    
  2. Click 이벤트 대리자 시그니처와 일치하는 이벤트 처리기를 정의합니다. Click 이벤트는 대리자 형식에 EventHandler 클래스를 사용하고 이벤트 데이터에는 EventArgs 클래스를 사용합니다.

    void Button_Click(object sender, EventArgs e)
    {...}
    
    Sub Button_Click(sender As Object, e As EventArgs)
    ...
    End Sub
    
  3. ButtonClick 이벤트에 이벤트 처리기 메서드를 추가합니다.

    button.Click += new EventHandler(this.Button_Click);
    
    AddHandler myButton.Click, AddressOf Me.Button_Click
    
    참고:

    Visual Studio 2005과 같은 디자이너에서는 이 예제의 코드와 비슷한 코드를 생성하여 이벤트 연결 작업을 자동으로 수행합니다.

예제

다음 코드 예제에서는 ButtonClick 이벤트를 처리하여 TextBox의 배경색을 변경합니다. 굵게 표시된 요소는 이벤트 처리기 코드와 이 이벤트 처리기가 ButtonClick 이벤트에 연결되는 방식을 보여 줍니다.

이 예제의 코드는 Visual Studio 2005과 같은 비주얼 디자이너를 사용하지 않고 작성되었으며 필수적인 프로그래밍 요소만 포함합니다. 디자이너를 사용하면 추가 코드를 생성할 수 있습니다.

[C#]

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
public class MyForm : Form 
{
   private TextBox box;
   private Button button;

   public MyForm() : base() 
   {  
      box = new TextBox();
      box.BackColor = System.Drawing.Color.Cyan;
      box.Size = new Size(100,100);
      box.Location = new Point(50,50);
      box.Text = "Hello";

      button = new Button();
      button.Location = new Point(50,100);
      button.Text = "Click Me";

      // To wire the event, create
      // a delegate instance and add it to the Click event.
      button.Click += new EventHandler(this.Button_Click);
      Controls.Add(box);
      Controls.Add(button);   
   }
   // The event handler.
   private void Button_Click(object sender, EventArgs e)    {      box.BackColor = System.Drawing.Color.Green;   }
   // The STAThreadAttribute indicates that Windows Forms uses the
   // single-threaded apartment model.
   [STAThreadAttribute]
   public static void Main(string[] args) 
   {
      Application.Run(new MyForm());
   }
}

[Visual Basic]

Option Strict On

Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing

Public Class MyForm
   Inherits Form
   Private box As TextBox
   Private WithEvents myButton As Button

   Public Sub New()
      box = New TextBox()
      box.BackColor = System.Drawing.Color.Cyan
      box.Size = New Size(100, 100)
      box.Location = New Point(50, 50)
      box.Text = "Hello"

      myButton = New Button()
      myButton.Location = New Point(50, 100)
      myButton.Text = "Click Me"

      AddHandler myButton.Click, AddressOf Me.Button_Click

      Controls.Add(box)
      Controls.Add(myButton)
   End Sub

   ' The event handler.
   Private Sub Button_Click(sender As Object, e As EventArgs)      box.BackColor = System.Drawing.Color.Green   End Sub

   ' The STAThreadAttribute indicates that Windows Forms uses the
   ' single-threaded apartment model.
   <STAThreadAttribute()> _
   Public Shared Sub Main(args() As String)
      Application.Run(New MyForm())
   End Sub
End Class

코드 컴파일

앞의 코드를 파일(C# 파일인 경우 확장명이 .cs이고 Visual Basic 2005의 경우 확장명이 .vb임)에 저장하고 컴파일한 다음 실행합니다. 예를 들어, 소스 파일의 이름이 WinEvents.cs(또는 WinEvents.vb)이면 다음 명령을 실행합니다.

csc /r:System.DLL /r:System.Windows.Forms.DLL /r:System.Drawing.DLL WinEvents.cs
vbc /r:System.DLL /r:System.Windows.Forms.DLL /r:System.Drawing.DLL WinEvents.vb

실행 파일의 이름은 WinEvents.exe가 됩니다.

참고 항목

개념

이벤트 및 대리자

이벤트 사용

이벤트 발생시키기

기타 리소스

이벤트 처리 및 발생