-
October 21st, 2007, 01:32 PM
#1
How do i add EventHandlers to my Inherited or custom classes?
Please vote the posts you find usefull.
---
I'm back
Bigger and badder
Better and bolder
Explaining stuff -
With an improved vocabulary!
-
October 21st, 2007, 02:02 PM
#2
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.
Sincerely,
Martin Svendsen
-
October 21st, 2007, 03:35 PM
#3
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
Code:
CustomForm.Ctick += new EventHandler(CustomForm_Ctick);
Get it?
Please vote the posts you find usefull.
---
I'm back
Bigger and badder
Better and bolder
Explaining stuff -
With an improved vocabulary!
-
October 21st, 2007, 04:15 PM
#4
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.
Code:
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).
Code:
Ctick(this, new EventArgs());
Sincerely,
Martin Svendsen
-
October 22nd, 2007, 01:49 AM
#5
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
Code:
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.
Code:
public delegate void EventTable(object sender, clsCustomEvenArgs e);
public event EventTable evtYourEvent;//For Custom events
Inside your Timer code
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
Ravi.Battula
-
October 22nd, 2007, 07:28 AM
#6
Re: How do i add EventHandlers to my Inherited or custom classes?
Just small correction: the event fire must be done this way:
Code:
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:
Code:
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.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|