Solved

How do I pass parameters/objects to an Eventhandler?

Posted on 2008-10-11
2
2,948 Views
Last Modified: 2013-12-17
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
Comment
Question by:Matt_Du
2 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
ID: 22695539
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
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 125 total points
ID: 22697370
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now