Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create a Custom Event Handling class Like EventArgs

Tags:

c#

how to write custom event handling classes, if any one having idea about how to create it or if you know any good article regarding this then please help me.

Thanks in Advance

like image 961
Nitish Katare Avatar asked Nov 23 '10 09:11

Nitish Katare


People also ask

What are the steps to write custom event in C#?

Steps of Event CreationCreate a delegate, which holds the details of the subscribers to an event. Create a public event that is externally visible to the class and used for creating subscriptions. Create the method in the class, which will fire the event itself.

What is EventArgs EC?

EventArgs e is a parameter called e that contains the event data, see the EventArgs MSDN page for more information. Object Sender is a parameter called Sender that contains a reference to the control/object that raised the event.

What is EventArgs in VB?

AssemblyLoadEventArgs class derives from EventArgs and is used to hold the data for assembly load events. To create a custom event data class, create a class that derives from the EventArgs class and provide the properties to store the necessary data. The name of your custom event data class should end with EventArgs .


1 Answers

I'm not entirely sure what you mean, but if you're talking about an EventArgs derived class:

public class MyEventArgs : EventArgs
{
    private string m_Data;
    public MyEventArgs(string _myData)
    {
        m_Data = _myData;
    } // eo ctor

    public string Data {get{return m_Data} }
} // eo class MyEventArgs


public delegate void MyEventDelegate(MyEventArgs _args);

public class MySource
{
    public void SomeFunction(string _data)
    {
        // raise event
        if(OnMyEvent != null) // might not have handlers!
            OnMyEvent(new MyEventArgs(_data));
    } // eo SomeFunction
    public event MyEventDelegate OnMyEvent;
} // eo class mySource

Hope this helps.

like image 70
Moo-Juice Avatar answered Oct 19 '22 09:10

Moo-Juice