Windows Forms Denetimlerinde Olay Tanımlama

Özel olayları tanımlama hakkında ayrıntılı bilgi için bkz . Olaylar. İlişkili verileri olmayan bir olay tanımlarsanız, olay verileri EventArgsiçin temel türü kullanın ve olay temsilcisi olarak kullanın EventHandler . Geriye kalan tek şey, olayı oluşturan bir olay üyesini ve korumalı Onbir EventName yöntemini tanımlamaktır.

Aşağıdaki kod parçası, özel denetimin FlashTrackBar özel olayı ValueChangednasıl tanımladığı gösterir. Örneğin tam kodu için FlashTrackBar bkz . Nasıl yapılır: İlerleme Durumunu Gösteren Bir Windows Forms Denetimi Oluşturma.

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);  
   }  
}  

Ayrıca bkz.