.NET Windows Formアプリケーションでフォームの名称を変更するとフォームデザイナの編集内容が失われる、編集内容が反映されない

.NET Windows Formアプリケーションでフォームの名称を変更するとフォームデザイナの編集内容が失われる、編集内容が反映されない現象と対処法を紹介します。

現象

フォームの名称を変更後に、フォームデザイナでフォームを編集して保存しても、編集内容がアプリケーションの実行結果に反映されない場合があります。

原因

いくつかの原因が考えられますが、よくある原因としてフォームのリネーム時に、フォームデザイナのコードは新しい名称のクラス名に変更できたが、 フォームのコードのクラス名が古いままになっているため、問題が発生するケースがあります。

この場合、InitializeComponentメソッドがデザイナのコードではなく、フォームのコードに生成される問題もあります。

問題となるコード

作成したForm1を FormNewName に変更した際に問題が発生しました。
問題発生時のコードが以下です。

FormNewNameに変更していますが、FormNewName.cs ファイルのクラス名が Form1のままになっており、その後のフォームデザイナの編集内容が、 FormNewName.Designer.csのInitializeComponent メソッドではなく、FormNewName.csにInitializeComponentメソッドが作成され、 編集内容がデザイナのコードでないコードに反映されてしまっています。

FormNewName.cs
namespace MyApp
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void InitializeComponent()
    {
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(12, 12);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(75, 23);
      this.button1.TabIndex = 0;
      this.button1.Text = "button1";
      this.button1.UseVisualStyleBackColor = true;
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(12, 41);
      this.button2.Name = "button2";
      this.button2.Size = new System.Drawing.Size(75, 23);
      this.button2.TabIndex = 1;
      this.button2.Text = "button2";
      this.button2.UseVisualStyleBackColor = true;
      // 
      // Form1
      // 
      this.ClientSize = new System.Drawing.Size(398, 189);
      this.Controls.Add(this.button2);
      this.Controls.Add(this.button1);
      this.Name = "Form1";
      this.ResumeLayout(false);

    }

    private Button button1;

    private void button1_Click(object sender, EventArgs e)
    {
      //クリックイベント
    }

    private Button button2;
  }
}

FormNewName.Designer.cs
namespace MyApp
{
  partial class FormNewName : Form
  {
    /// <summary>
    ///  Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    ///  Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null)) {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    ///  Required method for Designer support - do not modify
    ///  the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(800, 450);
      this.Text = "Form1";
    }

    #endregion
  }
}

対処法

クラス名を揃え、InitializeComponent メソッドの内容、コントロールの宣言をフォームデザイナのコードに移動します。

FormNewName.cs
namespace MyApp
{
  public partial class FormNewName : Form    //Form1 → FormNewName 
  {
    public Form1()
    {
      InitializeComponent();
    }
    
    // InitializeComponent メソッドを削除

    // コントロールの宣言のコードを削除

    private void button1_Click(object sender, EventArgs e)
    {
      //クリックイベント
    }
  }
}

FormNewName.Designer.cs
namespace MyApp
{
  partial class FormNewName : Form
  {
    /// <summary>
    ///  Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    ///  Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null)) {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    ///  Required method for Designer support - do not modify
    ///  the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(12, 12);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(75, 23);
      this.button1.TabIndex = 0;
      this.button1.Text = "button1";
      this.button1.UseVisualStyleBackColor = true;
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(12, 41);
      this.button2.Name = "button2";
      this.button2.Size = new System.Drawing.Size(75, 23);
      this.button2.TabIndex = 1;
      this.button2.Text = "button2";
      this.button2.UseVisualStyleBackColor = true;
      // 
      // Form1
      // 
      //this.components = new System.ComponentModel.Container();    //不要?
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(398, 189);
      this.Controls.Add(this.button2);
      this.Controls.Add(this.button1);
      this.Name = "Form1";
      this.Text = "Form1";
      this.ResumeLayout(false);
    }

    #endregion

    //コントロールの宣言をデザイナのコードに移動
    private Button button1;
    private Button button2;
  }
}

対策:この状態にならないようにする

この状況にならないようにするには、フォームのデザイナを開いた状態で、フォームのファイル名の変更をしないようにします。
次の手順で処理します。

  1. Windows Formのプロジェクトを作成する
  2. プロジェクトが作成されると、ソリューションエクスプローラーに作成したプロジェクトが追加され、メインフォーム(Form1)がフォームデザイナで開かれる
  3. すぐにフォームの名前を変更せずに、フォームデザイナやフォームのコードを閉じる
  4. フォームのファイルが閉じられた状態で、フォームのファイル名を変更する
  5. ファイル名変更後にフォームデザイナを開いて編集する


上記の方法で正しくファイル名が変更できます。さらに入念に慎重に作業する場合には次の手順を実施します。

  1. Windows Formのプロジェクトを作成する
  2. プロジェクトが作成されると、ソリューションエクスプローラーに作成したプロジェクトが追加され、メインフォーム(Form1)がフォームデザイナで開かれる
  3. すぐにフォームの名前を変更せずに、フォームデザイナやフォームのコードを閉じる
  4. フォームのファイルが閉じられた状態で、フォームのファイル名を変更する
  5. ソリューションを一度閉じる
  6. ソリューションを再度開く
  7. ファイル名をリネームしたプロジェクトをリビルドする
  8. フォームデザイナを開く
AuthorPortraitAlt
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
作成日: 2023-01-20