Question

I'm going through my code and redoing all my events so they are in compliance with this article but I've run into a wall. Here's my code:

Script.cs

public EventHandler<ScriptEvent> Load;

protected virtual void OnLoad(string file)
{
    EventHandler<ScriptEvent> handler = Load;

    if(Load != null)
       Load(this, new ScriptEvent(file));
}

and ScriptEvent:

ScriptEvents.cs

public class ScriptEvent : EventArgs
{
    private string m_File;

    public string File
    {
        get { return m_File; }
    }        

    public ScriptEvent(string file)
    {
       this.m_File = file;
    }
}

The problem is I cannot figure out how to make it so my form can handle the events. For instance, a file is loaded inside of Script.cs, then the form displays the file when OnLoad(...) inside of Script.cs is called. if that's confusing:

1) Script.cs loads a file and triggers OnLoad(file)

2) Form1 picks up on OnLoad and does whatever.

I figured it would be something similar to the way I was going it before (script.OnLoad += script_OnLoad(...)) but I'm stumped.

Was it helpful?

Solution

I think you might want:

script.Load += script_OnLoad;

This will add your handler to the event. Then when the OnLoad method is called, it will invoke your handler.

Your script_OnLoad method needs to look like this:

void script_OnLoad(object sender, ScriptEvent args)
{
     // your code here - you can get the file name out of the args.File
}

Your Script.cs can be cut down to:

public EventHandler<ScriptEvent> Load;

protected virtual void OnLoad(string file)
{
    if(Load != null)
        Load(this, new ScriptEvent(file));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top