0

Possible Duplicate:
Passing arguments to event handler

I'm trying to handle an event raised from a MenuItem.Click. The catch is, I need some way to pass some custom arguments with it (specifically, two integers representing row and column).

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    ...
    MenuItem froNewChild = new MenuItem();
    froNewChild.Header = "Insert new box";
    froNewChild.Click += new RoutedEventHandler(froNewChild_Click);
    // froNewChild.Click += new RoutedEventHandler(froNewChild_Click, column, row);
    FlowRectangleOptions.Items.Add(froNewChild);
    ...
}

private void froNewChild_Click(object sender, RoutedEventArgs e) //+column & row
{
    FlowDocument.allColumns[column-1].AddFlowRectangle(column, row);
    FlowDocument.ReRenderAll(canvas1);
}

This answer Passing arguments to an event handler would seem to do what I want, but doesn't work as-is because sender is already defined in this scope. Sadly I don't know enough yet to work around that problem - advice greatly appreciated!

Community
  • 1
  • 1
Steven Batham
  • 39
  • 2
  • 6
  • Oh, always the way, try at something for hours and then figure it out 4 minutes after posting the question. Ignore this! But thanks if you're reading this for taking an interest :) – Steven Batham Jan 03 '13 at 21:49
  • were do the values `column` an `row` come from? is there a `DataGrid` somewhere? – sa_ddam213 Jan 03 '13 at 21:50
  • Just for the future reference of anyone reading this, the solution was as described in http://stackoverflow.com/questions/12293471/passing-arguments-to-event-handler but I finally twigged that "sender" could be named something else! – Steven Batham Jan 03 '13 at 21:56

3 Answers3

2

Assumming "column" and "row" are created in your loaded method, you could just use a Lambda event handler so you can pass the variables in.

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        //Assumming "column" and "row" are in this block somewhere.
        ...
        MenuItem froNewChild = new MenuItem();
        froNewChild.Header = "Insert new box";
        froNewChild.Click += (s, eArgs) =>
        {
            FlowDocument.allColumns[column - 1].AddFlowRectangle(column, row);
            FlowDocument.ReRenderAll(canvas1);
        };
        FlowRectangleOptions.Items.Add(froNewChild);
        ...
    }
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
0

The argument passed to the evant handler depends on how the raising class is designed, and they are passed as a class derived from EventArgs as a second argument. Augmenting this is not possible unless you are the designer of the class raising the event. In this particular case we are talking about a framework class and so it is not possible at all.

As a workaround you can define some state member variable in your class and use them to pass informations to the handler.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
0

sa_ddam213 already answered you but just to clarify: the parameter names in lambda expressions carry no semantic meaning and can be whatever you want. The names sender and eventArgs were only used for clarity in the question you linked. What matters in this case is that there are two params and the compiler infers the rest by himself.

The values of column and row will be captured at the point you define the lambda and will be available when the event handler executes even if MainWindow_Loaded has long ago returned and the original variables destroyed.

For more about lambda expressions in C#: http://msdn.microsoft.com/en-us/library/bb397687.aspx