简体   繁体   English

不使用VS设计器时,是否应该向Form1.designer.cs添加代码?

[英]When not using VS designer, should I add code to Form1.designer.cs?

I am not sure about that. 我不确定。 I do not want to use designer for many things (but for some I do). 我不想在很多事情上使用设计师(但对于某些人来说)。 Should I write my "manual" code into the designer.cs or to form class itself? 我应该将我的“手动”代码写入designer.cs还是形成类本身?

No, never. 没有永不。

Because it will be generated again and your written-code will be lost. 因为它将再次生成,您的书面代码将丢失。

In order to add code to a class which has designer, you need to define a partial class with the same name. 为了将代码添加到具有设计器的类中,您需要定义具有相同名称的部分类。

public partial class Form1
{
    // here write your logic (which will be preserved).
}

For more info: Partial Class (C#) 有关更多信息: 部分类(C#)

You should really not do that. 你真的不应该那样做。 Each time you change something in the designer, it will regenerate the InitializeComponent() method. 每次在设计器中进行更改时,它都会重新生成InitializeComponent()方法。 Therefore, your changes will be overwritten by that code. 因此,您的更改将被该代码覆盖。 The only way this would not happen is to write your code in the same way the designer does - which is almost impossible. 唯一不会发生这种情况的方法就是以设计人员的相同方式编写代码-这几乎是不可能的。 It could also happen that your designer crashes after you changed the code and you may not know why. 更改代码后,设计师也可能崩溃,并且您可能不知道为什么。 You should better stick with the designer. 你应该更好地坚持设计师。

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

相关问题 为什么 C# 设计器生成的代码(如 Form1.designer.cs)会对 Subversion 造成严重破坏? - Why does C# designer-generated code (like Form1.designer.cs) play havoc with Subversion? form1.designer.cs和form1.resx的目的 - purpose of form1.designer.cs and form1.resx 无论如何,如果我通过C#中的表单设计器编辑表单,禁止删除我在Form1.Designer.cs中所做的更改? - Anyway to forbid deleting my changes made in Form1.Designer.cs if i edit form via Form Designer in C#? 我有一个 .NET C# Windows 表单应用程序在 Form1.Designer.cs 上出错 - I have a .NET C# Windows Form app erroring on Form1.Designer.cs Visual Studio毫无意义地编辑我的Form1.Designer.cs - Visual Studio editing my Form1.Designer.cs without no sense c#中form1.cs,form1.designer.cs和program.cs之间的区别 - Difference between form1.cs, form1.designer.cs and program.cs in c# 自定义组合框不会在Form1.designer.cs中保存项目集合 - Custom combobox doesn't save Item Collection in Form1.designer.cs 使用 VS 设计器时不需要的表单修改 - Unwanted Form modifications when using VS designer 我在vs 2012中使用数据集,但是在table.adapter中查看表适配器时无法获得代码 - I am using dataset in vs 2012 but view tableadapter not ganerate code in designer.cs 使用实体框架时的designer.cs - designer.cs when using entity framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM