Using Events

Declaring, subscribing to, and firing events using EventHandler<T>.

Includes

#include <System/EventHandler.hpp>
#include <System/EventArgs.hpp>

Step 1: Define EventArgs

Create a class that inherits System::EventArgs to carry event data:

class ButtonClickArgs : public System::EventArgs {
public:
    int x, y;
    ButtonClickArgs(int x, int y) : x(x), y(y) {}
    GetTypeNameHPP();
};

// In .cpp:
GetTypeNameCPP(ButtonClickArgs, "UI.ButtonClickArgs")

If the event carries no data, use System::EventArgs directly.

Step 2: Declare the Event

Add an EventHandler<T> public field to your class:

class Button : public System::Object {
public:
    System::EventHandler<ButtonClickArgs> Click;
    GetTypeNameHPP();

    void press(int x, int y);
};

Step 3: Fire the Event

void Button::press(int x, int y) {
    if (!Click.Empty()) {
        Click.Raise(this, ButtonClickArgs(x, y));
    }
}

Always check Empty() before raising to avoid firing with zero subscribers (while not harmful, it saves the function call overhead).

Step 4: Subscribe

Button btn;

// Subscribe with a lambda
btn.Click += [](System::Object* sender, ButtonClickArgs args) {
    std::cout << "Clicked at " << args.x << "," << args.y << "\n";
};

// Subscribe with a named function
void onButtonClick(System::Object* sender, ButtonClickArgs args) {
    // ...
}
btn.Click.Add(onButtonClick);

// Fire
btn.press(10, 20); // calls all subscribers

Multiple Subscribers

Button btn;

btn.Click += [](System::Object* sender, ButtonClickArgs args) {
    std::cout << "Handler 1\n";
};
btn.Click += [](System::Object* sender, ButtonClickArgs args) {
    std::cout << "Handler 2\n";
};

btn.press(0, 0);
// Output:
// Handler 1
// Handler 2

Checking Subscriber Count

int count = btn.Click.Size();   // number of subscribers
bool empty = btn.Click.Empty(); // true if no subscribers

EventArgs with No Data

When the event carries no extra data, use System::EventArgs directly:

class Timer : public System::Object {
public:
    System::EventHandler<System::EventArgs> Tick;
    GetTypeNameHPP();
    void fire() {
        if (!Tick.Empty())
            Tick.Raise(this, System::EventArgs{});
    }
};

Timer t;
t.Tick += [](System::Object*, System::EventArgs) {
    std::cout << "Tick!\n";
};

Comparison with C#

C#sharp-runtime
event EventHandler<T> E;EventHandler<T> E;
E += handler;E += handler; (same)
E?.Invoke(sender, args);if (!E.Empty()) E.Raise(sender, args);
E -= handler;E.Remove(handler);