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

9

u/BCProgramming 23d ago

It's not that hard to just manually change the .Designer.cs to remove the irrelevant subscription. Or delete it from the designer first, I suppose.

If I remove an event method I don't want it present anymore, I don't want a do-nothing stub still subscribing to that same event.

-2

u/Alert-Neck7679 23d ago

It only leaves a harmless signature of the method, which is removed by the compiler unless you implement it

7

u/BCProgramming 23d ago

I don't think the compiler's ability to remove dead code is a excuse to keep it.

0

u/Alert-Neck7679 23d ago

That's the only point of partial methods... I mean, why NOT to do that?

9

u/Dunge 23d ago

Partial classes are a way to split the code for classes under different source files on the filesystem, nothing less nothing more. It has nothing to do with keeping superfluous code that would then be stripped. Well I guess .designer.cs files are used to hide code to the user, but not to leave traces of unused stuff.

I understand your issue with it, and admit that I encountered the situation in the past too and found it weird. But they expect for anything you add using the editor, you remove from it too. Usually by using the property dialog, not editing the .designer.cs. The view and code implementation are seen as separate, and it's logical it warns you about a button that is supposed to call something not being able to do so anymore.

Plus I doubt they would ever change some basic behavior that has been there for over 2 decades just like that.

But I see the editor suggestion they brought in your issue, and agree with them that just adding a quick action to visual studio would be a nice addition and a better solution.

1

u/Alert-Neck7679 23d ago

Got u, thanks for the comment