Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winforms in .NET Core 6.0 issue with VS 2022

I am trying to add control to a Winforms application running on .NET Core 6.0 in VS 2022 Community edition. Whenever I add a control from the toolbox, like a button or a label, and try to save the project, an error message pops up

Should have two or more class parts

There's no further information. Here's a screenshot or reference:

enter image description here

Any ideas?

like image 498
sansknwoledge Avatar asked Oct 24 '25 16:10

sansknwoledge


1 Answers

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”
like image 63
S. Gomez Avatar answered Oct 27 '25 06:10

S. Gomez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!