O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required