Skip to content

Partial methods as controls events #14650

@ArcadeMakerSources

Description

@ArcadeMakerSources

Background and motivation

Currently, when you have a control, say a button, in the visual form designer, and you double-click it, it generates a normal method in the the MyForm.cs file, such as:

private void button_Click(object sender, EventArgs e)
{

}

And in MyForm.designer.cs it just adds

this.button.Clicked += new EventHanlder<EventArgs>(this.button_Click);

Now, if you will remove this method, it will cause a designer error, and you'll have to go to the designer code and manually delete this line. For beginners it's especially problematic...

I think that a better approach would be to generate partial method, and add its signature in the designer:

this.button.Clicked += new EventHanlder<EventArgs>(this.button_Click);
...
partial void button_Click(object sender, EventArgs e);

This way, if you will delete the method implementation, it won't be an error.

API Proposal

partial class Form1 : Form
{
    private partial void Button1_Click(object sender, EventArgs e)
    {

    }
}

API Usage

Form1.designer.cs:

private void InitializeComponent()
{
    ...
    this.Button1.Clicked += new System.EventHandler<System.EventArgs>(this.Button1_Click);
}

private Button Button1;
private partial void Button1_Click(object sender, EventArgs e);

Alternative Designs

No response

Risks

No response

Will this feature affect UI controls?

The VS designer will need to generate event methods as partial methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestion(1) Early API idea and discussion, it is NOT ready for implementationuntriagedThe team needs to look at this issue in the next triagewaiting-author-feedbackThe team requires more information from the author

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions