Jun 6, 2009

C# Events Tutorial

In the class that throws the events:

First declare a delegate:

private delegate void AudioIsPlayingHandler(MediaElement elem);


Then declare an event:

private event AudioIsPlaingHandler OnAudioIsPlaying;


Somewhere, the class throws the event:

if (OnAudioIsPlaying != null)
OnAudioIsPlaying(elem); // Raise the event and pass some data


Now, in a class that has an instance of the previous class:

First, attach to the event in obj A, an instance of the previous class:

A.OnAudioIsPlaying += new System.EventHandler(A_OnAudioIsPlaying);


Then, in the handler function:

private void A_OnAudioIsPlaying(MediaElement elem)
{
// Do something with the data received
}

Finally, check this out for a lot more details:
Visual Studio Developer Center - Events Tutorial

No comments:

Post a Comment