This is simple. Your only problem is that you seemingly know how to use the already declared events, handle them. It means, you know the usage without understanding of events, otherwise you would know how to declare and invoke then. You try to act like a user of the class declaring an even, while you need to act like an author of such class.
All you need to know is: 1) declaration of an event instance as a member of class/structure (control class, in your case), 2) event invocation. Those are two items you never could do as a mere user of some class/structure. Not that the invocation of the event is only possible from its declaring class, impossible even from the derived class. (Many asked how to fire some certain event. It makes no sense at all. If event is already declared in a library, it cannot be invoked elsewhere: this limitation is the important fool-proof feature.)
You just need to start over about learning the events and perhaps delegates. Please start here:
http://msdn.microsoft.com/en-us/library/awbftdfh.aspx[
^].
The general patten is simple. First, you need to have some event argument class. Use one of available classes or derive your own one from
System.EventArgs
class, which is only needed when you have to pass your custom parameters to a handler of an event.
class MyEventArgs : System.EventArgs {}
Class MyEventArgs
Inherits System.EventArgs
End Class
Then, declare the control class:
public class MyControl : Control {
public event System.EventHandler<MyEventArgs> MyEvent;
public event System.EventHandler MySimlestEvent;
void InvokeMyEvent() {
if (MyEvent != null)
MyEvent.Invoke(this, new MyEventArgs());
}
void InvokeMySimplestEvent() {
if (MySimplestEvent != null)
MySimplestEvent.Invoke(this, new System.EventArgs());
}
}
Public Class MyControl
Inherits Control
Public Event MyEvent As System.EventHandler(Of MyEventArgs)
Public Event MySimlestEvent As System.EventHandler
Private Sub InvokeMyEvent()
If MyEvent IsNot Nothing Then
MyEvent.Invoke(Me, New MyEventArgs())
End If
End Sub
Private Sub InvokeMySimplestEvent()
If MySimplestEvent IsNot Nothing Then
MySimplestEvent.Invoke(Me, New System.EventArgs())
End If
End Sub
End Class
It does not matter if it is control class or not, it can be a structure, too. This is the universal recommended pattern. You can use some custom pattern without "sender" and "eventArgs"; it will work, but FxCop won't approve it (
http://en.wikipedia.org/wiki/Fxcop[
^]).
—SA