Reputation: 2856
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
Upvotes: 0
Views: 3780
Reputation: 11357
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
}
}
Upvotes: 3
Reputation: 7594
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);
}
Upvotes: 2
Reputation: 415860
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.
Upvotes: 0
Reputation: 57902
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.
Upvotes: 1
Reputation: 3972
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'!
Upvotes: 1