Solved

How do I pass parameters/objects to an Eventhandler?

Posted on 2008-10-11
2
2,946 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

11 Experts available now in Live!

Get 1:1 Help Now