I want to show a text in a label for a particular time in my form and this is the code i tried so far :
private void ShowTextForParticularTime(String caption)
{
Timer t = new Timer { Interval = 2000, Enabled = true };
t.Tick += new EventHandler(OnTimerEvent(caption));
}
private void OnTimerEvent(object sender, EventArgs e,String caption)
{
barStaticItem3.Caption = caption;
}
My question is how can I set the “caption” parameter into OnTimerEvent method because that code I wrote doesn’t work it gives me this error :
No overload for method ‘OnTimerEvent’ takes ‘1’ arguments
Use this instead:
Reason is you need to assign some event handler to the event. But when you state
new EventHandler(OnTimerEvent(caption));you are actually trying to invoke it. The invocation call at compile time of course fails because the method requires 3 parameters (sender, e, caption).If instead you create an anonymous delegate via lamdas, you can take advantage of their syntax and closure to wire the event while passing in your third
captionparameter.