I’m learning how to work with delegates and by now have got some ideas. In a C# code (below) I like to capture type of event in string. What is the best approach to get the source of event and type of event?
For name of the source I’m using sender.GetType().FullName.ToString(); if it is correct. What about event type?
Thanks.
protected virtual void OnChanged(EventArgs e)
{
if (Changed != null)
Changed(this,e);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
OnChanged(EventArgs.Empty);
}
public EventListener(myForm theform)
{
TheForm = theform;
TheForm.Changed += new ChangedEventHandler(myMethod);
}
private void myMethod(object sender, EventArgs e)
{
string s = "hey, got notified " + sender.GetType().FullName.ToString();
MessageBox.Show(s);
}
There is no way to know which event call the function with the
EventArgsclass.If you are using the prepared events (like Click event), you can create your own “Args” class like that:
Then call the event like that:
And in the receiver method you can see that value, like that:
But, if you are using your own event you can create your own delegate and do with it whatever you want.