Basic Design-Code question with Visual Studio/C#


[Visual Studio version 17.8.3 — Community 2022]

I have used Visual Studio quite a bit to write VB programs. I am now attempting to use it to write C# programs. (I have a background in Java as well, so the coding isn’t an issue.) I am having trouble with the designer-code interplay.

I am getting a designer error when I don’t think I should. Is there a “proper” way to entirely delete an event-handler function if it is unwanted?

Scenario:
(1) In Designer, I add a label and a button to a form.
(2) I accidentally double-click on the label instead of the button when adding an event-handler
(3) I go to the code and delete the auto-added Click function code for the label. (Which shouldn’t have any code tied to it.)
(4) I go back to the designer and it has produced a “Design-Time” error. (The name ‘label1_Click’ does not exist in the current context )

I can use undo to put the function back and the error goes away. (Then I’m stuck with an empty function.) I can delete the label, then delete the code and all is okay. (But, if I have spent some time setting properties for the label, deleting it because I accidentally mis-clicked is painful.)

Leave a Reply

Your email address will not be published. Required fields are marked *