简体   繁体   English

在 Visual Studio 2022 中,意外重命名事件处理程序会破坏表单设计器

[英]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通常,当您更改事件名称(不是在属性窗口中)时,它会出现此错误: 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 ).但是,当我升级到 VS 2022 时,如果我更改了事件处理程序代码 ( changed code ),然后打开我的设计器窗口,我会看到这个 ( 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:编辑 1:
-Tried rebuilding, didn't fix the Designer window. - 尝试重建,但没有修复 Designer 窗口。
-Tried restarting VS after fix and it still didn't work. -修复后尝试重新启动 VS,但仍然无效。
-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 - 来不及使用 ctrl-Z
-I also tried deleting the event from both Form1.Designer.cs and Form1.cs Image of Form1.Designer.cs code -我还尝试从 Form1.Designer.cs 和 Form1.cs 中删除事件Form1.Designer.cs 代码的图像

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 ).好像是不显示Form1,Text本来是设置成Form1的,但是报错后Text是空的(我说的就是这个)。 But the Design name is still Form1但设计名称仍然是 Form1

In order to rename the names of events, variables, methods, etc. use the keyboard shortcut Ctrl + R + R .要重命名事件、变量、方法等的名称,请使用键盘快捷键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 .有关位于表单上的控件的所有信息(实例、位置、颜色、大小、事件等)都集中在 * .Designer.cs中的InitializeComponent()方法中。 The fact that you have an empty form displayed means that your form is most likely using a different Form1.Designer.cs .您显示一个空表单这一事实意味着您的表单很可能使用不同的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.按住Ctrl键,将鼠标悬停在构造函数中调用的InitializeComponent方法的名称上,然后单击鼠标左键。 Visual Studio will take you to the place where the InitializeComponent method is described. Visual Studio 将带你到描述InitializeComponent方法的地方。

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.如果您的项目中仍有原始的Form1.Designer.cs ,您可以简单地复制其InitializeComponent方法并在较新的设计器脚本中替换此方法。

I've had the same problem.我有同样的问题。

After investigation of InitializeComponent() I found that all the statements this.Controls.Add(...) were missing.在调查InitializeComponent()之后,我发现所有的语句this.Controls.Add(...)都丢失了。

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;)至少这教会了我总是使用 VS 的重命名/重构功能而不是 hacky 方式;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Visual Studio 2022 的程序集中定义基本表单时,无法在表单设计器中显示继承的表单 - Cannot Display Inherited Form in Form Designer When Base Form Defined in an Assembly for Visual Studio 2022 Visual Studio 2022 对 .xsd 设计器执行搜索 - Visual studio 2022 perform search into .xsd designer Visual Studio 2022 自动完成和粘贴中断格式化 - Visual Studio 2022 autocompletion and pasting breaks formatting 如何在 Visual Studio 2022 和 EF 6 中使用 EF 设计器? - How to use the EF Designer in Visual Studio 2022 and EF 6? Visual Studio 2022 17.3.6 Xaml Designer 未针对 UNO 平台显示 - Visual Studio 2022 17.3.6 Xaml Designer not showing up for UNO platform Visual Studio 2015,删除事件处理程序和设计表单错误 - Visual studio 2015, remove event handler and error of design form JsonApiDotNetCore 5.0.1 与最新版本的 Visual Studio 2022(版本 17.3.0)中断 - JsonApiDotNetCore 5.0.1 Breaks with latest version of Visual Studio 2022 (Version 17.3.0) Visual Studio 2017:Windows 窗体设计器未出现 - Visual Studio 2017 : Windows Form designer not appear Visual Studio中的表单设计器会覆盖我的代码 - Form designer in visual studio overrides my code 如何在 Visual Studio 中打开表单设计器? - How to open form designer in Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM