This page is likely outdated (last edited on 11 May 2011). Visit the new documentation for updated content.

MonoMac/Documentation/Events

Events

If you want to intercept events from UIControl, you have a range of options: from using the C# lambdas and delegate functions to using the low-level Objective-C APIs.

The following shows how you would capture the TouchDown event on a button, depending on how much control you need:

C# Style

Using the delegate syntax:

NSButton button = MakeTheButton ();
button.Activated += delegate {
    Console.WriteLine ("Touched");
};

If you like lambdas instead:

button.Activated += (o, e) => {
   Console.WriteLine ("Touched");
};

If you want to have multiple buttons use the same handler to share the same code:

void handler (object sender, EventArgs args)
{
   if (sender == button1)
      Console.WriteLine ("button1");
   else
      Console.WriteLine ("some other button");
}
 
button1.Activated += handler;
button2.Activated += handler;