question

NicholasPiazza-0093 avatar image
0 Votes"
NicholasPiazza-0093 asked AnnaXiu-MSFT commented

Windows Forms Designer Problem When Renaming New Form Before Adding Controls

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
 }

}

vs-generalwindows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello, I tested on my side and couldn’t reproduce your issue. Are you using the latest version of VS2022? Can you tell me the specific version? Community or Professional or Enterprise?
You can try to repair your VS through Visual Studio Installer > More > Repair and create a new Windows Forms project to try again.

0 Votes 0 ·

1 Answer

NicholasPiazza-0093 avatar image
0 Votes"
NicholasPiazza-0093 answered AnnaXiu-MSFT commented

I figured out the workaround today. I created a solution with two test projects. I created a Windows Forms project which, of course, defaulted to names Form1.cs and Form1.Designer.cs. Then before adding controls, I renamed the files to FormTest1.cs, etc. I then dragged three controls (a Button, a Label, and a TextBox) from the Toolbox to the form. I built and ran the project and, as I have seen before, the form was displayed with no controls. I closed the application and examined the FormTest1.Designer.cs file and found that there was no information about the controls I had dragged in.

I then created a second Windows Forms project with the default name of Form1.cs. This time, before renaming the form I dragged the same three controls as in the previous project to the form. Then I renamed the form file to FormTest2.cs. I built and ran the project and this time the form was displayed with all three forms. I closed the application and examined the FormTest2.Designer.cs file and found that all the information about the dragged in controls was present.

It seems Visual Studio just can't handle renaming the Form files before adding controls.

You can see the projects I created to demonstrate this on GitHub at https://github.com/nick1941/DesignerTest. Project1 is the result of renaming before adding controls. Project2 is the result of renaming after adding controls.

I have reported this problem to the Microsoft Visual Studio development team.

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Can you post a link to your bug report here so that others can easily find it.

0 Votes 0 ·

I submitted the report using Visual Studio Feedback. The link is : https://developercommunity.visualstudio.com/home?port=1025&fsid=34a2691d-a776-447b-966f-0dc4352b6ff6&entry=myfeedback

The title is: Windows Forms Designer Problem When New Form is Renamed

1 Vote 1 ·