dotnet / dotnet/winforms

Suggested Feature: Designer Support for Missing Event Handlers

Open
#14,650 9 comments 1 reaction 0 assignees View on GitHub
api-suggestion untriaged
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

### 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(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(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

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

}
}
```

### API Usage

Form1.designer.cs:

```csharp
private void InitializeComponent()
{
...
this.Button1.Clicked += new System.EventHandler(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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.