?
Solved

How do I pass parameters/objects to an Eventhandler?

Posted on 2008-10-11
2
Medium Priority
?
2,953 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 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 500 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

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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