Raise a Custom Event

Problem

You need to send data with a custom event.

Solution

Derive a custom event argument object from the System.EventArgs class, add the information you need, and declare the event. Use RaiseEvent to fire the event.

Discussion

.NET events follow a common syntax. The first argument is the sender of the event (as a weakly typed System.Object instance). The second argument is an object derived from System.EventArgs that contains any additional information. The custom EventArgs class should always be named in the form EventNameEventArgs.

Here’s an event declaration that follows these conventions and can be added to the declaration of any class:

Public Event TaskCompleted(sender As Object, e As TaskCompletedEventArgs)

The custom EventArgs ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.