How do i add EventHandlers to my Inherited or custom classes?

How do i add EventHandlers to my Inherited or custom classes?
[2 byte] By [dahwan] at [2007-11-20 11:34:09]
# 1 Re: How do i add EventHandlers to my Inherited or custom classes?
I don't understand your question. "EventHandler" is just a delegate, usually used to manage events thrown by forms or controls.
Homogenn at 2007-11-9 11:36:49 >
# 2 Re: How do i add EventHandlers to my Inherited or custom classes?
Well, F. inst.

I have inherited a form and added a timer. I want an Event on each tick, so when i create the form, it'd go like

CustomForm.Ctick += new EventHandler(CustomForm_Ctick);

Get it? :)
dahwan at 2007-11-9 11:37:49 >
# 3 Re: How do i add EventHandlers to my Inherited or custom classes?
Put this in the form's code. Then you can subscribe to the event you call "Ctick" with an EventHandler delegate.

public event EventHandler Ctick;

To fire the event, write something like (depends obviously on what you want to be sender and what EventArgs you want).

Ctick(this, new EventArgs());
Homogenn at 2007-11-9 11:38:48 >
# 4 Re: How do i add EventHandlers to my Inherited or custom classes?
Hi,
If you want to creare totally new custom event handler,
First create a public event class like this

public class clsCustomEvenArgs : EventArgs
{
public string m_sEvent1;
public string m_sEvent2;
public clsEventTableArgs(string sEvent1, string sEvent2)
{
m_sEvent1= sEvent1;
m_sEvent2= sEvent2;
}
}
//This is public event handler. sEvent1,sEvent2 are events which you want to send *** argumets.

inside your other code where you want to rise these events., define one parametirised delegate "clsCustomEvenArgs " as an argument. Also define one event like this.

public delegate void EventTable(object sender, clsCustomEvenArgs e);
public event EventTable evtYourEvent;//For Custom events

Inside your Timer code

private void timer1_Tick(object sender, EventArgs e)
{
if(/*your condition)
{
clsCustomEvenArgs args = new clsCustomEvenArgs ((string)arg1, (string)arg2);//Pass your arguments
evtYourEvent(this, args);//For custom event data
}
}

This will do

Regards
Ravi.Battula
battula32 at 2007-11-9 11:39:54 >
# 5 Re: How do i add EventHandlers to my Inherited or custom classes?
Just small correction: the event fire must be done this way:

if (evtYourEvent != null) evtYourEvent(this, args)

because the evtYourEvent has not to be set, so you could get NullReferenceException.

If you would like to be absolutely sure and correct, you should use:

EventTable tmpEvent = evtYourEvent;
if (tmpEvent != null) tmpEvent(this, args)

because someone can accidentaly remove the last handler from the event between the null check and the invocation.
boudino at 2007-11-9 11:40:53 >