Visual Studio: Handling renaming of items with design view

FriQenstein 136 Reputation points
2020-10-03T16:51:08.687+00:00

Greetings,

Let's say that we are creating a form in design view in Visual Studio. We place a button on the form and then double-click the button to automatically generate an event handler for the click of the button. This takes us directly to the code view of the handler where we write our code, save the solution, and then run it to test.

Ok, now, initially, we forgot to rename the button's name, the variable name not the text within the button.
However, the code within the project is now saved as an event handler for button1 instead of an actual adequate name. We can rename the button within the Properties tab however, the event handler does not change.

I am assuming that VS creates some sort of symbolic link or pointer from the new button name to the one that already exists within the code for the even handler.
How can we change this? Even if we delete the original button, create a new one and rename it before putting our code in the event handler, the old button's even handler is still present in the code, and obviously somewhere else in the project.

What's the easiest way to clear this up?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,837 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.5K Reputation points
    2020-10-03T17:07:29.09+00:00

    In case of C#, the event handlers are subscribed using “+=” operator inside a special file called like Form1.Designer.cs, in InitializeComponent function. It is maintained by Visual Studio and is rarely edited manually. I think that it parses this source file to determine the details of controls.

    To rename the handler after renaming the control, you can place the caret inside the function name, press <F2> (or Edit, Refactor, Rename menu), and type or edit the new name.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. FriQenstein 136 Reputation points
    2020-10-03T18:15:53.907+00:00

    @Viorel
    Thank you very much. That worked perfectly.
    I usually am fairly adamant about renaming things prior to making any changes/save, but occasionally it happens.

    Thanks again for this help. Much appreciated.

    Regards.

    0 comments No comments