简体   繁体   中英

In Visual Studio 2022 accidentally renaming an event handler breaks the Form Designer

Usually, when you change an event name ( not in the properties window), it will come up with this error: Error Image

However, when I upgraded to VS 2022, if I changed the event handler code ( changed code ) and then open my designer window, I'm greeted to this ( a broken designer window ).

Even if I reverted the event handler name change, the window would stay the same. I just want the form to go back to how it looked with all my buttons, menu-strips, etc. ( What the window should look like )

Please help this inexperienced coder;(

Edit 1:
-Tried rebuilding, didn't fix the Designer window.
-Tried restarting VS after fix and it still didn't work.
-Didn't use refactor, but after correcting the name the designer window is still broken (yes I checked the designer file)
-It's too late to use ctrl-Z
-I also tried deleting the event from both Form1.Designer.cs and Form1.cs Image of Form1.Designer.cs code

It's like it's not displaying Form1, The text used to be set to Form1, but after the error the Text is empty ( This is what I'm talking about ). But the Design name is still Form1

In order to rename the names of events, variables, methods, etc. use the keyboard shortcut Ctrl + R + R .

All information about the controls located on your form ( instances, positions, colors, sizes, events, etc. ) is concentrated in the InitializeComponent() method in * .Designer.cs . The fact that you have an empty form displayed means that your form is most likely using a different Form1.Designer.cs .

Go to the script of your form, find the constructor there:

...

public Form1()
{
   InitializeComponent();
}

...

Hold down the Ctrl key, hover over the name of the InitializeComponent method called in the constructor, and click the left mouse button. Visual Studio will take you to the place where the InitializeComponent method is described.

Check if there is information about those buttons that you showed in one of the images. Most likely, this information will not be there. If you still have the original Form1.Designer.cs in your project, you can simply copy its InitializeComponent method and replace this method in a newer designer's script.

I've had the same problem.

After investigation of InitializeComponent() I found that all the statements this.Controls.Add(...) were missing.

Re-adding them makes both the Form and the Designer work as before. I have no clue why this happened, but it's nice to know there is an easy fix.

At least this taught me to always use the rename/refactor function of VS instead of the hacky way;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM