views:

89

answers:

1

Im using WPF which has a Storyboard class that has a Completed event.

I use it like so:

sb.Completed += AddControlToTaskbar;

private void AddControlToTaskbar(object sender, EventArgs args)
{
    //...
}

How to I pass in the EventArgs to my method? Its always null, and I need it to be a custom class

Thanks Mark

+4  A: 

You don't pass the EventArgs to your method, the framework which dispatches the event does that. A common way to handle this is to wrap up your AddControlToTaskbar method in a class which stores the state, e.g.:

sb.Completed += new MyCustomClass(theStateYouNeedToStore).AddControlToTaskbar;

Your constructor stores the state.

class MyCustomClass<T> {
    private T state;
    public MyCustomClass(T state) {
        this.state = state;
    }
    public void AddControlToTaskbar(object sender, EventArgs args) {
        // Do what you need to do, you have access to your state via this.state.
    }
}
nullptr
ok, and is the AddControlToTaskbar a public delegate of that class?
Mark
I've expanded the example, hopefully it's clearer.
nullptr
thanks for that... but the next thing I need to do is to make sure that after I run the `AddControlToTaskbar` method, I remove the handler from the Storyboard... how would I do that from the `AddControlToTaskbar` method in another class?
Mark
Pass it (`sb`) to your MyCustomClass constructor as well, then you have access to call whatever you want/need on it.
nullptr
Done and done... thanks for the help!
Mark