john true
john true

Reputation: 273

Add class to current event handler

I want to add class to current event handler.

Original Event parameter like this;

void ugTopActivity_ChartDataClicked(object sender, ChartDataEventArgs e)

I want to add another class like below FillSceneGraphEventArgs ;

private void ugTopActivity_ChartDataClicked(object sender, ChartDataEventArgs e, FillSceneGraphEventArgs ex)
{
    box = new Box(test);
    ex.SceneGraph.Add(box);
}

However it throws an error like;

'No overload for matches delegate'

How can I pass it?

EDIT-1

private void ugTopActivity_ChartDataClicked(object sender, ChartDataEventArgs e)
{            
    var o = sender as FillSceneGraphEventArgs;   --> 'Object not set instance of an object' 

    IAdvanceAxis x = (IAdvanceAxis)o.Grid["X"];

    IAdvanceAxis y = (IAdvanceAxis)o.Grid["Y"];

    Box box = new Box(new Infragistics.Win.DataVisualization.Rectangle((double)x.Map(0), (double)y.Map(1), 50, 371));
    box.PE.Fill = Color.Black;
    box.PE.FillOpacity = 70;

    if (o != null)
    {
        o.SceneGraph.Add(box);
    }
}

Upvotes: 0

Views: 63

Answers (3)

Salah Akbari
Salah Akbari

Reputation: 39966

If you need additional argument, first go to your event declaration and then you can use lambda. It should be something like this:

ugTopActivity.ChartDataClicked += new ChartDataEventHandler((sender , e) =>
   ugTopActivity_ChartDataClicked(sender,e,new FillSceneGraphEventArgs()));

Upvotes: 1

John Wu
John Wu

Reputation: 52290

The proper way

The intended approach is to use the EventArgs parameter to pass a class containing all the information you need. So in this case you'd have to extend ChartDataEventArgs, or possibly provide your own class that inherits from EventArgs.

A sneaky way

On the other hand, if you need to expose some piece of information, but it isn't something that changes with each event, you could also just expose it as a public property of the class that raised the event. Then you could access it by casting the sender. Your example doesn't tell me what type the sender is, but say it was a MyChartClass, you could do this:

class MyChartClass
{
    public FillSceneGraph GetFillSceneGraph()
    {
        get 
        {
            return _somethingOrOther;
        }
    }
}

Then update the handler like so:

private void ugTopActivity_ChartDataClicked(object sender, ChartDataEventArgs e)
{
    var o = sender as MyChartClass; //Or whatever type it is
    if (o != null) 
    {
        var box = new Box(test);
        var sceneGraph = o.GetFillSceneGraph();  //Get the scene graph from the sender
        sceneGraph.Add(box);  //Add the box to the graph
    }
}

Upvotes: 2

Julo
Julo

Reputation: 1120

Event handler has a defined count of parameters.

Standardly when you need to add new data/'return value', it is added to the argument class, in this case ChartDataEventArgs class.

You can not add a new parameter, since the caller will not be able to call a method with more parameters than expected. It always calls method with 'object sender' and 'arguments'. What should be there for the next parameter?

When you are the implementer of the caller you can change it, but the prefered way is with update of the arguments class.

Upvotes: 2

Related Questions