Function Request: Use ExposeEvents and Handles in the Winforms designer

As the ExposeEvents and Handles aspects work properly now (Function Request: ExposeEvents and Handles), I come with my last VB.Net related request (honest :wink: )

Current workings
When I create a form in the designer with a button on it, the following code is generated:

type
  MainForm = partial class
  {$REGION Windows Form Designer generated code}
  private
    components: System.ComponentModel.Container := nil;
      button1: System.Windows.Forms.Button;
    method InitializeComponent;
  {$ENDREGION}
  end;

When I double click the button, the following code is added:

  method button1_Click(sender: System.Object; e: System.EventArgs);

and in the designer:

  self.button1.Click += new System.EventHandler(self.button1_Click);

Drawback of the current workings is that when I remove the button from the form:

  • I have to edit the designer.pas to remove the event wiring
  • No warnings or errors during compile on the - still existing - button_Click implementation (which is now dead code).

Requested workings
When I create a form in the designer with a button on it, the following code is generated:

type
  MainForm = partial class
  {$REGION Windows Form Designer generated code}
  private
    components: System.ComponentModel.Container := nil;
    [ExposeEvents]button1: System.Windows.Forms.Button; //Expose the events of the button
    method InitializeComponent;
  protected
    [ExposeEvents]var Me := self; //Expose the events of the form itself 
  {$ENDREGION}
  end;

When I double click the button, the following code is added:

  [Handles(button1.Click)]method button1_Click(sender: System.Object; e: System.EventArgs);

and in the designer nothing is added

When I remove the button from the form:

  • I don’t have to remove any code in the designer as no code was added for the click event.
  • The button_Click implementation - which is now dead code - won’t compile anymore because of the handles aspect without the ExposeEvents variable.

This means less editing when removing components and no dead code left overs in this case.

It should be a very nice toutch when the event-method code is generated in the editor after typing the [Handles(component.event)], meaning that I do not have to remember the syntax of the event.

Example:
I type

[handles(button1.Validating)]

Press Enter, and the following code is added by Oxygene itself:

method button1_Validating(sender: Object; e: CancelEventArgs);

Allowing me to easily create the event handlers in code, without needing to remember or lookup the specific event delegate syntax.

The main problem I see is that the form designer would be aware of whether you’d want to use “your” VB mode, or not. I suppose we could make it dependent on the presence of [ExposeEvents]var Me := self; which you’d add yourself as a one-time step, too opt that form into the new mode?

Or just a setting?