CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2007
    Posts
    375

    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!

  2. #2
    Join Date
    Dec 2006
    Posts
    203

    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

  3. #3
    Join Date
    Mar 2007
    Posts
    375

    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!

  4. #4
    Join Date
    Dec 2006
    Posts
    203

    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

  5. #5
    Join Date
    Nov 2004
    Posts
    105

    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

  6. #6
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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
  •  





Click Here to Expand Forum to Full Width

Featured