c# - 如何阻止InitializeComponent更改表单设计器代码?

标签 c# visual-studio windows-forms-designer

我有一个在 InitializeComponent 中调用的覆盖方法。

public class ComponentResourceManager2 : System.ComponentModel.ComponentResourceManager

我的问题是,每次我在 DesignerForm 中更改某些内容时,InitializeComponent 方法都会更改,以便该方法使用系统方法

System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager();

例如:我需要这个

    private void InitializeComponent()
    {
        ComponentResourceManager2 resources = new ComponentResourceManager2(typeof(Form1));
        this.directDrawViewer1 = new MyOwn.Imaging.Components.DirectDrawViewer();
        this.speedButton1 = new MyOwn.Components.SpeedButton();
        ...

但每次我更改某些内容时,它都会再次出现:

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        this.directDrawViewer1 = new MyOwn.Imaging.Components.DirectDrawViewer();
        this.speedButton1 = new MyOwn.Components.SpeedButton();

我怎样才能使我的方法被InitializeComponent使用,而无需每次更改时都重置?

最佳答案

您不应该更改设计器代码,在调用 InitializeComponent() 后,在 Form 的构造函数中设置您在 InitializeComponent 中设置的内容。每次您在设计器中更改某些内容时,Visual Studio 都会重新生成设计器文件

关于c# - 如何阻止InitializeComponent更改表单设计器代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46937326/

相关文章:

c# - 装箱 int 不会将其转换为引用类型?

c++ - 任何人都知道如何修复编译错误 : LNK2005?(内部源代码)

visual-studio - 是否可以使用 VS Azure 资源组项目将 ASP.NET Core 网站部署到 Azure?

c# - Outlook 加载项和 Windows 窗体协作

c# - 限制向 ToolStrip 添加控件

c# - 具有自动高度属性的用户控件

C# - 快速分配所有字段/属性的代码片段或模板?

c# - 检查年份是否为闰年

c# - ZedGraph 的多 X 轴

c# - Visual Studio : executing clean up code when debugging stops