• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2959
  • Last Modified:

How do I pass parameters/objects to an Eventhandler?

I am confused as to how I have to pass parameters/objects to an Eventhandler?

I have:

linklabel.click  += new EventHandler(hwIDForm);
linklabel is a LinkLabel type object.

which points to a method with signature:

private static void hwIDForm(object sender, EventArgs e){}

I derived a new class from EventArgs and added to it the ability to return the objects I want to pass, but I am at a loss as to how I pass it to hwIDForm when I define the event, there are no overloads to specify.
Any help is appreciated. MSDN
0
Matt_Du
Asked:
Matt_Du
2 Solutions
 
Jaime OlivaresCommented:
in this case, you can't. the click event has a defined argument class. What you you want to pass? you can put some info at linklabel's Tag property, so you can retrieve as:

object info = (sender as Control).Tag;
0
 
Anurag ThakurCommented:
As already mentioned by Jaime Olivares you cannot use the click event. In order to use your own event you have to take the following steps

How to Raise an Event
At a minimum, you must do three things to raise an event:

Create a delegate:
// C#
public delegate void MyEventHandler(object sender, EventArgs e);

Create an event member:
// C#
public event MyEventHandler MyEvent;

Invoke the delegate within a method when you need to raise the event, as the following code demonstrates:
// C#
MyEventHandler handler = MyEvent;
EventArgs e = new EventArgs();
if (handler != null)
{
    // Invokes the delegates.
    handler(this, e);
}
// Note that C# checks to determine whether handler is null.

Additionally, you can derive a custom class from EventArgs if you need to pass information to the event handler.

0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now