Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete the generated function from accidentally double clicking on a button? [duplicate]

I am working on a forms application with a button. When I double click on it on the designer form, it automatically creates a private void saveButton_Click() function.

How do I delete this function without generating errors in Visual Studio 2012? If I delete the method, it will create an error that states "File does not contain a definition for saveButton_click() and no extension method 'save_button_click'. So how would I delete this method completely as I accidentally created this when I double clicked on the button on designer form?

I can't undo as I have done other changes before trying to fix this. Should I delete something from another file in Visual Studio 2012?

EDIT: I cannot undo (CTRL - Z) as I have saved the files and done other changes as stated above. What I am looking for is a way for Visual Studio to remove all event handlers of when the button is double clicked automatically.

like image 639
SamIAm Avatar asked Dec 10 '13 10:12

SamIAm


2 Answers

You see this error message, because Visual Studio adds two things when you are double-clicking button:

1) It creates empty event handler in code file

void saveButton_click(object sender, EventArgs e)
{

}

2) In designer generated code file it subscribes this handler to button click event

saveButton.Click += new EventHandler(saveButton_click);

When you remove handler, you still have it referenced in designer file. And compiler can't find saveButton_click method which you just removed.

If accidental button click is the only change you made, then just click Ctrl-Z and operation will be reverted (both handler and event subscription will be removed). In this case you will see this dialog box, don't afraid to click Yes

enter image description here

Otherwise you should remove subscription (either manually, by editing designer file, or from designer) and remove handler (only manually).

like image 87
Sergey Berezovskiy Avatar answered Oct 31 '22 02:10

Sergey Berezovskiy


Winforms - Visually remove button click event

This shows how to remove event handlers using Visual Studio 2012. F4 and under the properties tab.

like image 25
SamIAm Avatar answered Oct 31 '22 02:10

SamIAm