Listeners and .Net

I think the instructions for using Listeners or events in the .Net make it sound more complicated than it is. I was reading some text on them when I was working on the spellchecker inside Help Writer to refresh my mind since I don’t use them often. And they just made it more confusing than I remember in class.

I think its far simpler to think of it like this. Inside your control, you need to create an event and a delegate:

public delegate void ReplaceActionDone(string oldtext, string newtext);
public event ReplaceActionDone Replace;

Now the delegate should have the same parameters as the methods that your going to attach to the event in the code outside the control.

In the control’s code you’re going to need to invoke the event where it should perform the necessary code:

Replace?.Invoke(variables.MispelledWord, variables.AcceptedChange);

Now in the parent’s code, you need to set up the methods:

public void ReplaceEvent(string oldText, string newtext)
{

// code to be done

}

And then you need to add it to the control’s event so it fires:

scdialog.Replace += ReplaceEvent;

 

And that’s all there really is to getting an event listener in net.