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)
   {  
       onValueChanged?.Invoke(this, e);  
   }  
}  

참고 항목