Windows Forms 컨트롤에서 이벤트 정의

사용자 지정 이벤트 정의에 대한 자세한 내용은 이벤트 발생시키기를 참조하십시오. 연결된 데이터가 없는 이벤트를 정의하는 경우 이벤트 데이터의 기본 형식인 EventArgs를 사용하고 EventHandler를 이벤트 대리자로 사용합니다. 그런 다음 이벤트 멤버와 해당 이벤트를 발생시키는 보호된 OnEventName 메서드를 정의하면 됩니다.

다음 코드 부분에서는 FlashTrackBar 사용자 지정 컨트롤이 사용자 지정 이벤트 ValueChanged를 정의하는 방법을 보여 줍니다. FlashTrackBar 샘플의 전체 코드를 보려면 방법: 진행률을 보여 주는 Windows Forms 컨트롤 만들기를 참조하십시오.

Option Explicit
Option Strict

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

Public Class FlashTrackBar
   Inherits Control
   
   ' The event does not have any data, so EventHandler is adequate 
   ' as the event delegate.        
   ' Define the event member using the event keyword.
   ' In this case, for efficiency, the event is defined 
   ' using the event property construct.
   Public Event ValueChanged As EventHandler
   ' The protected method that raises the ValueChanged 
   ' event when the value has actually 
   ' changed. Derived controls can override this method.  
   Protected Overridable Sub OnValueChanged(e As EventArgs)
      RaiseEvent ValueChanged(Me, e)
   End Sub
End Class
using System;
using System.Windows.Forms;
using System.Drawing;

public class FlashTrackBar : Control {
   // The event does not have any data, so EventHandler is adequate 
   // as the event delegate.
   private EventHandler onValueChanged;
   // Define the event member using the event keyword.
   // In this case, for efficiency, the event is defined 
   // using the event property construct.
   public event EventHandler ValueChanged {
            add {
                onValueChanged += value;
            }
            remove {
                onValueChanged -= value;
            }
        }
   // The protected method that raises the ValueChanged
   // event when the value has actually 
   // changed. Derived controls can override this method.  
   protected virtual void OnValueChanged(EventArgs e) {
      if (ValueChanged != null) {
         ValueChanged(this, e);
      }
   }
}

참고 항목

개념

Windows Forms 컨트롤의 이벤트

이벤트 발생시키기

기타 리소스

이벤트 처리 및 발생