views:

236

answers:

5

Hi! I have this array with information

 string[] aInfo; // Contains information about this person

I have a clickevent on a menuitem that goes like this:

    // Event for adding contact
    mContact.Click += new EventHandler(addContactMenuClick);

Then further into the project i have the eventhandler:

    // Eventhandler for adding a contact
    private void addContactMenuClick(object sender, System.EventArgs e)
    {
        string sNumber = ((MenuItem)sender).Text;
        MessageBox.Show(sNumber);
    }

How can i send the array with the event?

All this is in a foreach loop, so the array will never be the same.

Thanks in advance

+3  A: 

Define your own class that derives from EventArgs and add a field for your array there.

class MyEventArgs : EventArgs
{
    public string[] Info { get; private set; }

    public MyEventArgs(string[] info)
    {
        this.Info = info;
    }
}

EDIT (thanks to the comments):

OF course, you cannot change the information that the Click-Event sends, because you can'T change to code that fires the event.

One possible solution would be to derive your own MenuItem and add the information array as a field to this derived class. Then provide a property for this field, so you can access it via the sender parameter of your event. Example:

class MyMenuItem : MenuItem
{
    public string[] Info { get; private set; }

    public MyMenuItem(string[] strInfo)
    {
        this.Info = strInfo;
    }
}

Access:

protected void mnu_Click(object sender, EventArgs e)
{
    MyMenuItem obj = sender as MyMenuItem;

    if (obj != null)
    {
        //Access
    }
}
Maximilian Mayerl
I assume the click event is not one he defined so he can't dictate what EventArgs get sent?
Steven Robbins
That's correct, of course. And even if he yould, the event gets fires by the control, so he can't change the call. But he could, for example, subscripe to the click event and then fire his own event with his own EventArgs.
Maximilian Mayerl
Yes, but then he needs a way to know what the instance of that custom EventArg type would look like.
Joel Coehoorn
He would have to know that anyway, since the information he wants to add is defined by him. No existing event could know what to do with it. But that of course makes you think if an event is the right approach. He could keep his contact information array in a list with the user as a key.
Maximilian Mayerl
@Max - sure, but that's not easily inferred from your post so it's only really half an answer.
Steven Robbins
I'll edit it so it becomes clearer.
Maximilian Mayerl
+1  A: 

Just put it in a custom Event Args object! Just create an object (ContactMenuClickedEventArgs) or something that inherits from Event Args.

Wrap mContact in another object, and override the 'click' functionality. Have it create the custom EventArgs, and it will get passed in as 'e'!

Erich
+1  A: 

You can't really add the information to an existing event type. But you can derive your own EventArgs class (ContactAddedEventArgs) that can hold any information you like, and set up a new event (ContactAdded) that accepts this new data.

If you then wish to also Add contacts when you get a click, then just make the click event handler create the user info array (ContactAddedEventArgs) and then raise your ContactAdded event with it.

Jason Williams
A: 

The key is in the comment to this line:

string[] aInfo; // Contains information about this person

"this person". So aInfo is a member of some class, correct? And when you click your addContact MenuItem you presumably want to use some currently selected object on the screen as your "contact-to-add". So you need to know how to do that part (find the currently selected object on the screen). This object will likely have some relationship to the type containing your aInfo member, and if it doesn't you need to fix that.

Joel Coehoorn
+2  A: 

each control has a Tag property that is of the "object" datatype. you could assign your array to this property. Then in the event, cast it back to an array, and use it.

string[] aInfo; // Contains information about this person

....
....

// Event for adding contact
mContact.Click += new EventHandler(addContactMenuClick);
mContact.Tag = aInfo;

// Eventhandler for adding a contact
private void addContactMenuClick(object sender, System.EventArgs e)
{
    string sNumber = ((MenuItem)sender).Text;
    string[] aInfo = (string[])((MenuItem)sender).Tag;
    MessageBox.Show(sNumber);
}
Eclipsed4utoo