Visual C++ .NET/Delegate/Event — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:06, 26 мая 2010
Содержание
Event firer and handler
#include "stdafx.h"
using namespace System;
ref class MyEventArgs : EventArgs
{
public:
property String^ Info;
MyEventArgs(String^ info)
{
Info = info;
}
};
delegate void MyEventHandler(Object^ sender, MyEventArgs^ args);
ref class EventSender
{
public:
event MyEventHandler^ MyEvent;
void Fire(MyEventArgs^ args)
{
MyEvent(this, args);
}
};
ref class EventReceiver
{
public:
void OnMyEvent(Object^ sender, MyEventArgs^ args)
{
Console::WriteLine("My Event with info: " + args->Info );
}
void SetUpToReceive(EventSender^ sender)
{
sender->MyEvent += gcnew MyEventHandler(this, &EventReceiver::OnMyEvent);
}
};
int main()
{
EventReceiver^ receiver = gcnew EventReceiver();
EventSender^ sender = gcnew EventSender();
receiver->SetUpToReceive(sender);
MyEventArgs^ myargs = gcnew MyEventArgs("abc");
sender->Fire(myargs);
}
event type
#include "stdafx.h"
using namespace System;
ref class MyEventArgs : EventArgs
{
public:
property String^ Info;
MyEventArgs(String^ info)
{
Info = info;
}
};
delegate void MyEventHandler(Object^ sender, MyEventArgs^ args);
ref class EventSender
{
public:
event MyEventHandler^ MyEvent;
void Fire(MyEventArgs^ args)
{
MyEvent(this, args);
}
};
ref class EventReceiver
{
public:
void OnMyEvent(Object^ sender, MyEventArgs^ args)
{
Console::WriteLine("My Event with info: " + args->Info );
}
void SetUpToReceive(EventSender^ sender)
{
sender->MyEvent += gcnew MyEventHandler(this, &EventReceiver::OnMyEvent);
}
};
int main()
{
EventReceiver^ receiver = gcnew EventReceiver();
EventSender^ sender = gcnew EventSender();
receiver->SetUpToReceive(sender);
MyEventArgs^ myargs = gcnew MyEventArgs("abc");
sender->Fire(myargs);
}
Extends EventArgs
#include "stdafx.h"
using namespace System;
ref class MyEventArgs : EventArgs
{
public:
property String^ Info;
MyEventArgs(String^ info)
{
Info = info;
}
};
delegate void MyEventHandler(Object^ sender, MyEventArgs^ args);
ref class EventSender
{
public:
event MyEventHandler^ MyEvent;
void Fire(MyEventArgs^ args)
{
MyEvent(this, args);
}
};
ref class EventReceiver
{
public:
void OnMyEvent(Object^ sender, MyEventArgs^ args)
{
Console::WriteLine("My Event with info: " + args->Info );
}
void SetUpToReceive(EventSender^ sender)
{
sender->MyEvent += gcnew MyEventHandler(this, &EventReceiver::OnMyEvent);
}
};
int main()
{
EventReceiver^ receiver = gcnew EventReceiver();
EventSender^ sender = gcnew EventSender();
receiver->SetUpToReceive(sender);
MyEventArgs^ myargs = gcnew MyEventArgs("abc");
sender->Fire(myargs);
}
Fire events and event handlers
#include "stdafx.h"
using namespace System;
using namespace System::Threading;
ref class MyEventArgs : EventArgs
{
public:
property String^ Info;
MyEventArgs(String^ info)
{
Info = info;
}
};
delegate void MyEventHandler(Object^ sender, MyEventArgs^ args);
ref class Events
{
public:
event EventHandler^ Start;
event EventHandler^ Exit;
void RaiseStartEvent()
{
Start(this, gcnew EventArgs());
}
void RaiseExitEvent()
{
Exit(this, gcnew EventArgs());
}
void OnStart(Object^ sender, EventArgs^ args)
{
Console::WriteLine("Starting");
}
void OnExit(Object^ sender, EventArgs^ args)
{
Console::WriteLine("Exiting");
}
};
void f(Events^ e)
{
e->RaiseStartEvent();
Console::WriteLine("Doing something.");
e->RaiseExitEvent();
}
int main()
{
Events^ events = gcnew Events();
// add the event handlers for Start and Exit
events->Start += gcnew EventHandler(events, &Events::OnStart);
events->Exit += gcnew EventHandler(events, &Events::OnExit);
f(events);
// remove the event handlers
events->Start -= gcnew EventHandler(events, &Events::OnStart);
events->Exit -= gcnew EventHandler(events, &Events::OnExit);
}
Register event handler
#include "stdafx.h"
using namespace System;
ref class EventSender{
public:
event EventHandler^ MyEvent;
void Fire(EventArgs^ args)
{
MyEvent(this, args);
}
};
ref class EventReceiver{
public:
void OnMyEvent(Object^ sender, EventArgs^ args){
Console::WriteLine("My Event");
}
void AddHandler(EventSender^ sender){
sender->MyEvent += gcnew EventHandler(this, &EventReceiver::OnMyEvent);
}
};
int main(){
EventReceiver^ receiver = gcnew EventReceiver();
EventSender^ sender = gcnew EventSender();
receiver->AddHandler(sender);
EventArgs^ args = gcnew EventArgs();
sender->Fire(args);
}