I have the following EventHandler to which I added a parameter MusicNote music:
public void PlayMusicEvent(object sender, EventArgs e,MusicNote music)
{
music.player.Stop();
System.Timers.Timer myTimer = (System.Timers.Timer)sender;
myTimer.Stop();
}
I need to add the handler to a Timer like so:
myTimer.Elapsed += new ElapsedEventHandler(PlayMusicEvent(this, e, musicNote));
but get the error:
“Method name expected”
EDIT: In this case I just pass e from the method which contains this code snippet, how would I pass the timer’s own EventArgs?
Timer.Elapsedexpects method of specific signature (with argumentsobjectandEventArgs). If you want to use yourPlayMusicEventmethod with additional argument evaluated during event registration, you can use lambda expression as an adapter:Edit: you can also use shorter version: