Windows Forms Designer Problem When Renaming New Form Before Adding Controls

Nicholas Piazza 541 Reputation points
2022-09-14T20:22:54.647+00:00

Note: I have reported the problem below in Visual Studio 2022 Feedback. Just wanted to see if any of you have a suggestion.

Normally, when you create a new Form in Windows Forms and add controls to the form, the Designer automatically fills in the Designer.cs file with information about those controls, properties set, etc. However, If you create a new form and before adding controls you rename the form from its default name of Form (Form.cs, etc.) when you add controls and set properties for those controls in the form the Designer does not update the new form Designer.cs file. Then when you build and run the application, the displayed form is empty; none of the controls that were added using the Designer appear there because the Designer.cs file didn’t record the information. This is very frustrating. At this point, one either has to delete the form and start over, adding controls to the default Form.cs file and only afterwards renaming it, or creating a new InitializeComponent method from scratch doing all the work yourself. I would like the VS Designer to be smart enough to know that if the form is renamed before adding controls that the Designer.cs file should still be filled in correctly when controls are added to the form. Shown below is the minimal Designer.cs file for a form that was renamed to ConvertLength.
namespace MathHelper
{
partial class ConvertLength
{
/// <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 = "DialogLength";  
    }  

    #endregion  
}  

}

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,918 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,340 questions
{count} vote

11 answers

Sort by: Most helpful
  1. John Tarr 0 Reputation points
    2023-08-14T19:06:34.85+00:00

    I was seeing the same thing. I like to name my forms/controls before adding code to them. I tried a variety of things to no avail. Nicolas's answer of changing the form name, adding the name to InitializeComponent, saving, closing VS, and restarting - worked like a charm. Shouldn't need to do that though. Never had to on previous versions of VS.

    I had even tried the "Windows Forms App (.NET Framework)" option but ran into a different problem. It appeared to work as far as changing the form name fine. The problem came when I added a custom DLL. I could set the reference fine, add the using clause, and type ahead worked, but if I clicked the button with the code behind - I would get a System.IO.FileNotFoundException: 'Could not load file or assembly' error out in the Application.Run(new Main()); call in Program.cs.

    I know Windows Forms apps aren't popular these days but I think these 2 Windows Forms choices need to be tested a little more thoroughly.

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. John Tarr 0 Reputation points
    2023-08-14T19:07:32.1566667+00:00

    I was seeing the same thing. I like to name my forms/controls before adding code to them. I tried a variety of things to no avail. Nicolas's answer of changing the form name, adding the name to InitializeComponent, saving, closing VS, and restarting - worked like a charm. Shouldn't need to do that though. Never had to on previous versions of VS.

    I had even tried the "Windows Forms App (.NET Framework)" option but ran into a different problem. It appeared to work as far as changing the form name fine. The problem came when I added a custom DLL. I could set the reference fine, add the using clause, and type ahead worked, but if I clicked the button with the code behind - I would get a System.IO.FileNotFoundException: 'Could not load file or assembly' error out in the Application.Run(new Main()); call in Program.cs.

    I know Windows Forms apps aren't popular these days but I think these 2 Windows Forms choices need to be tested a little more thoroughly.

    0 comments No comments

  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  5. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.