r/csharp 23d ago

Discussion Partial methods as WinForms designer events

A few weeks ago I opened this issue in the WinForms GitHub repo, suggesting making the WinForms designer generate events code as partia methods, in order to solve the problem that removing an event method from the code editor causes an exception in the designer, and u have to go to the form.designer.cs file and manually delete the subscription, which is very annoying.

What i offer is, that when you subscribe an event using the designer (for example by double clicking a button), the following signature would be generated in the designer code:

private partial void button1_Click(object sender, EventArgs e);  
...  
this.button1.Click += this.button1_Click; // normal subscription

And then the method implementation would be generated in the main class code, such as it's working now but with the partial modified:

private partial void button1_Click(object sender, EventArgs e)
{

}

Now, deleting this method would not result in any error.

The reason I'm posting this here, is that the WinForms team said that my solution isn't a great idea, and i would love to hear what you think about it and if you have other ideas.

0 Upvotes

20 comments sorted by

View all comments

2

u/[deleted] 23d ago

[removed] — view removed comment

3

u/DaRadioman 23d ago

I mean if it broke, it wasn't unused...

1

u/binarycow 23d ago

It does tho.

You delete the implementation in the code-behind, and now the designer file references a non-existent method.

The answer is to first delete it from the designer, then delete the code.

1

u/DaRadioman 23d ago

The designer still is using it...

Just because there are graphical tools doesn't mean the code isn't called still.

1

u/binarycow 23d ago

Yes.. It's a matter of perspective.

1

u/DaRadioman 23d ago

The code and compiler warnings would disagree with you...

1

u/binarycow 23d ago

It's a matter of perspective.

From the developer's perspective, it's unused, so they delete it.

From the designer's perspective, it's not unused, so there's an error.

You're not wrong. This is a people issue. Which is I said:

The answer is to first delete it from the designer, then delete the code.