Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to dynamically subscribe and unsubscribe to a C# event in windows Forms Application. so that given a Object instance and a String name containing the name of the event, you subscribe to that event and do something when that event has been fired?

What I have tried:

string _EventHandler = string.Empty;

_EventHandler = cntrl + @"." + _EventName;


_EventHandler += new EventHandler(SaveData);

private void SaveData(object sender, EventArgs e)
{
// doing something
}
Posted
Updated 27-Feb-18 20:02pm
Comments
Richard MacCutchan 26-Feb-18 4:30am    
You need to use reflection to find the actual event reference from the string.

Not too difficult ... just messy, because you need to play around. Try this:
private void AddHandler(Control c, string eventName, EventHandler handler)
    {
    Type t = c.GetType();
    EventInfo ei = t.GetEvent(eventName);
    ei.AddEventHandler(c, handler);
    }
But I hope you have a good reason, because it's not going to improve your code readability...
 
Share this answer
 
Comments
Poornima Santhosh 27-Feb-18 0:00am    
Thank you I have tried this Its working if I have a method in the same class.

if I have a SaveData method in another class. how can i add it to the eventhandler?
OriginalGriff 27-Feb-18 3:11am    
Unless the event is part of the class to which it is being subscribed, you can't attach it - events don't work like that.
In order for an event handler to be called, the class that created the event has to very specifically run code to execute it:
https://www.codeproject.com/Articles/400287/A-simple-code-snippet-to-add-an-event
Shows the code you need to use in your class in order to get a handler to run.
Unless a class has the event to start with, that code would not be run, and the handler would never execute.
You can't attach a handler to an event the class doesn't expose: it doesn't exist and the handler would not be called if you could attach it!
Hi I got the solution here is my code

// Code for subscribing an event to call other class method
// code for adding a method in another class to the control event
// here _EventName is the name of the event (Click, Enter, Leave etc..)
// In MyEventsHandler class I have a SaveData method

EventHandler eHandler = new EventHandler(MyEventsHandler.SaveData);
AddHandler(cntrl, _EventName, eHandler);


//Function to add Eventhandler
private void AddHandler(Control c, string eventName, EventHandler handler)
{
try
{
Type t = c.GetType();
EventInfo ei = t.GetEvent(eventName);
ei.RemoveEventHandler(c, handler);
ei.AddEventHandler(c, handler);
}
catch (Exception ex)
{
MessageBox.Show(System.Reflection.MethodInfo.GetCurrentMethod().Name + " " + ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900