skip to Main Content

Yesterday, I started getting the "Should have two or more class parts." error when I adjust my UI through the Windows Forms Designer. This is on an established application. I then created a quick Win Forms application with a single button used to test some computational code through the debugger. I edit the code, execute the program, click the button, and use the debugger to walk through the code. This works fine. However, if I try to adjust the size of the button, or adjust the size of the Form1 window and try to Rebuild the application, I get the "Should have two or more class parts." error with no additional information. When I look at the Form1.Designer.cs file it retains the original settings prior to the attempted adjustment of the UI. If I close Visual Studio, I have to agree to lose any changes to the project. Upon reopening the Visual Studio project, my app runs fine, unless I try and change the UI in any way (adjust sizes, add controls, etc.).

I tried creating a new application in C# and the same error occurs. This application is as close to "Hello World" in its simplicity. I did note that yesterday Visual Studio was patched/upgraded. I suspect that may have something to do with the issue suddenly appearing on new applications as well as older, well-established, applications. I tried devenv /Safemode but that did not help.

Here is a section of my Designer.cs (below). When I edited this file directly to add the BackColor for button1 and the size of Form1, then save the Designer.cs file, and execute the program, all works perfectly. The error seems to be with the Windows Forms Designer.

I wanted to add the tag "net7.0-windows" but I don’t have enough reputation points to do that and that tag has not yet been created.

        private void InitializeComponent()
        {
            button1 = new Button();
            SuspendLayout();
            // 
            // button1
            // 
            button1.Location = new Point(28, 18);
            button1.Name = "button1";
            button1.Size = new Size(314, 105);
            button1.TabIndex = 0;
            button1.Text = "Calculate Tables";
            button1.BackColor = System.Drawing.Color.Orange;
            //button1.UseVisualStyleBackColor = true;
            button1.Click += button1_Click;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 15F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(400, 200);
            Controls.Add(button1);
            Name = "Form1";
            Text = "Form1";
            ResumeLayout(false);
        }

2

Answers


  1. Seems the same problem as reported in this thread. I posted a possible explanation and workaround. Copy-pasted here for visibility:

    In my case, the problem seems to be adding a second class in the form class file.

    Workaround: Define the new class in another file.

    Steps to reproduce the issue [Visual Studio 17.5.0 final]:

    1. Create a new project: Template: Windows Forms App (C#). Next. Set project name (i.e. MyApplication). Next. Framework: .NET 6.0 (Long Term Support). Create.
    2. (Visual Studio creates the project and opens Form1.cs in designer)
    3. Add a button control to form. Save (Ctrl+S).Ok.
    4. F7 to access form code.
    5. Add a second class (i.e. MyClass) below Form1 class, in the same
      namespace:
    namespace MyApplication
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
        }
    
        public class MyClass
        {
            public string Name { get; set; }
        }
    }
    
    1. Save (Ctrl+S). Ok.
    2. Return to designer and change anything (i.e. move the button
      position).
    3. Save (Ctrl+S) –> Error: “Should have two or more class parts
    Login or Signup to reply.
  2. If you have multiple Classes in a form, I tried cutting the non-form related ones out, saving the file, then adding them back in again and saving. Problem goes away till you moving anything on the form.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search