Answered How to remove useless methods?

glasswizzard

Active member
Joined
Nov 22, 2019
Messages
30
Programming Experience
Beginner
Hi, I have on occasion accidentally double clicked the wrong thing on my form and as such I have created several methods that are empty and not needed, the problem is when I delete these methods I get a compile error, so I'm obviously supposed to do more then simply delete text. An example of one of the method headers is "private void label3_Click(object sender, EventArgs e)".

Could someone tell me how to actually remove these method headers from my code properly please? It's a bit messy having to leave them there.
Thanks
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,506
Location
Sydney, Australia
Programming Experience
10+
The issue is that when you create an event handler that way, the IDE adds the method to the user code file and also adds an event handler registration to the designer code file. If you delete the method without deleting the registration, the registration becomes invalid because it refers to a method that doesn't exist. Rather than deleting the method manually, open the Properties window and click the Events button. You'll see your event handler selected for the event of interest. Right-click the event name and select Reset. That will remove both the registration and the method.

You won't be able to do that now that you've deleted the method, because the designer won't display because the designer code can't be compiled. That means that you'll need to delete the registration manually. To do that, open the designer code file from the Solution Explorer. It will be a child node of your form with a name ending in "Designer.cs". In that file, you should see a line of code like this:
C#:
this.label3.Click += new System.EventHandler(this.label3_Click);
Delete that line and build and you should be good to go.
 
Top Bottom