CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2010
    Posts
    5

    Unhappy Hook Event Handler

    Hey pals,

    I would like to know how to convert the following code:

    __hook(&CSource::MyEvent, pSource, &CReceiver::MyMethod);

    To the new managed c++/cli syntax.

    Very thanks!

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Hook Event Handler

    Create new C++/CLI Windows Forms application, add new button to the form and double-click on this button. Windows Form Designer adds this code to Form1.h:

    Code:
    this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    ...
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
    {
        ...
    }
    button1 is event source instance.
    Click is event
    "this" is event receiver instance
    button1_Click is receiver method used as event handler.

    Every .NET event has a type, which is defined by some delegate. You can define your own delegate or use existing one, like EventHandler. EventHandler defines the following type: Void (Object^, EventArgs^). Click event has EventHandler type. Event handler function should have the same type.

  3. #3
    Join Date
    Jul 2010
    Posts
    5

    Re: Hook Event Handler

    Thanks for your reply.
    But I don´t know how to convert the hook keyword into "+=".

  4. #4
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Hook Event Handler

    This is too much abstract. Show event definition you want to handle.

  5. #5
    Join Date
    Jul 2010
    Posts
    5

    Re: Hook Event Handler

    I want to handle this event:

    delegate void del_Event_Connect(UserConnect^ sender);
    event del_Event_Connect^ Event_Connect;

    And the hook for it:
    __hook(&CSource::Event_Connect, pSource, &CReceiver::MyMethod);
    Last edited by AntonioToledo; July 26th, 2010 at 01:00 PM.

  6. #6
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Hook Event Handler

    Code:
    EventSourceInstance->Event_Connect += gcnew del_Event_Connect(EventReceiverInstance, &MACHINE_SERVER::Form1::MachineLnk_Connect);
    
    Form1:
    void MachineLnk_Connect(UserConnect^ sender)
    {
        ...
    }
    Replace EventSourceInstance and EventReceiverInstance with references to required classes. For example, if subscription is done from Form1, EventReceiverInstance should be "this".

  7. #7
    Join Date
    Jul 2010
    Posts
    5

    Thumbs up Re: Hook Event Handler

    Now it works!
    Very thanks Alex!

  8. #8
    Join Date
    Aug 2010
    Posts
    51

    Smile Re: Hook Event Handler

    hi,


    Replace EventSourceInstance and EventReceiverInstance with references to required classes. For example, if subscription is done from Form1, EventReceiverInstance should be "this


    regards,
    phe9oxis,
    http://www.guidebuddha.com

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