|
-
July 19th, 2010, 06:51 AM
#1
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!
-
July 19th, 2010, 07:09 AM
#2
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.
-
July 19th, 2010, 01:13 PM
#3
Re: Hook Event Handler
Thanks for your reply.
But I don´t know how to convert the hook keyword into "+=".
-
July 19th, 2010, 11:54 PM
#4
Re: Hook Event Handler
This is too much abstract. Show event definition you want to handle.
-
July 20th, 2010, 07:26 AM
#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.
-
July 20th, 2010, 10:13 AM
#6
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".
-
July 20th, 2010, 12:27 PM
#7
Re: Hook Event Handler
Now it works!
Very thanks Alex!
-
August 12th, 2010, 02:15 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|