ExposesEvents

The ExposesEvents and Handles aspects make it easy to implement Event handlers and connect them to Fields of the class, especially when working with WinForms on .NET.

By annotating the field for control with ExposesEvents and marking each handler with Handles and the name of the event, the compiler will automatically generate code that connects the events, including taking care of readjusting the event assignments when the value of the field is changed to a different control. The field is implicitly upgraded to a Stored Property.

type
  MainForm = partial class(System.Windows.Forms.Form)
  public

    [ExposesEvents]
    btnHello: Button;

    [Handles(btn.Click)]
    method btnHello_Click(sender: System.Object; e: System.EventArgs);
    begin 
      MessageBox.Show('Hello');
    end;
    
  end;
public partial class MainForm : System.Windows.Forms.Form
{
    [ExposesEvents]
    Button btnHello;

    [Handles(btn.Click)]
    void btnHello_Click(System.Object sender, System.EventArgs e)
    {
        MessageBox.Show("Hello");
    } 
}
public __partial class MainForm : System.Windows.Forms.Form {
    
    @ExposesEvents
    Button btnHello

    @Handles(btn.Click)
    func btnHello_Click(_ sender: System.Object; _ e: System.EventArgs) {
        MessageBox.Show("Hello")
    } 
}
public __partial class MainForm : System.Windows.Forms.Form {

    @ExposesEvents
    Button btnHello;

    @Handles(btn.Click)
    void btnHello_Click(System.Object sender, System.EventArgs e) {
        MessageBox.Show("Hello");
    } 
}

This matches semantics of how Visual Basic.NET handles events.

See Also