Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing parameter to an event handler [duplicate]

Tags:

i want to pass my List<string> as parameter using my event

public event EventHandler _newFileEventHandler;     List<string> _filesList = new List<string>();  public void startListener(string directoryPath) {     FileSystemWatcher watcher = new FileSystemWatcher(directoryPath);     _filesList = new List<string>();     _timer = new System.Timers.Timer(5000);     watcher.Filter = "*.pcap";     watcher.Created += watcher_Created;                 watcher.EnableRaisingEvents = true;     watcher.IncludeSubdirectories = true; }  void watcher_Created(object sender, FileSystemEventArgs e) {                 _timer.Elapsed += new ElapsedEventHandler(myEvent);     _timer.Enabled = true;     _filesList.Add(e.FullPath);     _fileToAdd = e.FullPath; }  private void myEvent(object sender, ElapsedEventArgs e) {     _newFileEventHandler(_filesList, EventArgs.Empty);; } 

and from my main form i want to get this List:

void listener_newFileEventHandler(object sender, EventArgs e) {  } 
like image 914
user1269592 Avatar asked Dec 27 '12 17:12

user1269592


People also ask

How do you pass parameters to an event handler?

If you want to pass a parameter to the click event handler you need to make use of the arrow function or bind the function. If you pass the argument directly the onClick function would be called automatically even before pressing the button.

Is it possible to pass an additional parameter to an event handler?

Parameters of the even handlers are defined by the "event arguments" class (derived from System. EventArgs ); and you cannot change the signature of the event handler. So, you should have asked, "can I pass additional information?". Of course you can.

Is an EventHandler a delegate?

The EventHandler delegate is a predefined delegate that specifically represents an event handler method for an event that does not generate data. If your event does generate data, you must use the generic EventHandler<TEventArgs> delegate class.

What is the difference between event and EventHandler?

The event is the thing that RAISES an event, to which something will subscribe. The EventHandler is the thing that HANDLES an event - i.e. it specifies the method that is used to subscribe to the event.


1 Answers

Make a new EventArgs class such as:

    public class ListEventArgs : EventArgs     {         public List<string> Data { get; set; }         public ListEventArgs(List<string> data)         {             Data = data;         }     } 

And make your event as this:

    public event EventHandler<ListEventArgs> NewFileAdded; 

Add a firing method:

protected void OnNewFileAdded(List<string> data) {     var localCopy = NewFileAdded;     if (localCopy != null)     {         localCopy(this, new ListEventArgs(data));     } } 

And when you want to handle this event:

myObj.NewFileAdded += new EventHandler<ListEventArgs>(myObj_NewFileAdded); 

The handler method would appear like this:

public void myObj_NewFileAdded(object sender, ListEventArgs e) {        // Do what you want with e.Data (It is a List of string) } 
like image 91
Sawan Avatar answered Oct 01 '22 17:10

Sawan