Form inheritance crashes with .NET 6

789 Views Asked by At

I'm converting an old winforms application from 4.8 to 6.0.1. The conversion has been incredibly easy and the application worked with no problems at all until, suddenly, Visual Studio (2022 64-bit v17.0.5) refuses to show the form designer for every form inherits from another.

I scaled down my project until I discovered that it wasn't me...

using System.Windows.Forms;

namespace MyApp
{
    public partial class BaseForm : Form
    {
        public BaseForm()
        {
            InitializeComponent();
        }
    }
}

and

namespace MyApp
{
    public partial class ChildForm : BaseForm
    {
        public ChildForm()
        {
            InitializeComponent();
        }
    }
}

The other portion of every class (winforms splits the form class in two partials) are perfectly untouched.

The two forms are completely empty but the designer refuses to draw ChildForm returning this nice and senseless message:

The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: \r\n ChildForm --- The base class 'MyApp.BaseForm' could not be loaded. Ensure the assembly has been referenced and that all projects have been built. enter image description here

I've already tried everything I found on the Internet:

  • clean and rebuild the solution
  • exit from VS
  • put an explicit (and useless) :base() instruction on the ctor of the two forms
  • empty the cryptic folder C:\Users[me]\AppData\Local\Temp\WinFormsCache\

and many other tricks I found with no result at all.

The only things I haven't tried (yet) have been:

I haven't done those yet because the problem should be easy to fix. Reading the message it seems that the designer lost its ability to probe for assemblies and to read for types inside an assembly it have already referenced but this is true for an inherited form only. Crazy enough to investigate. Let's see if someone has already discovered the nth foolishness MS has been able to do.

1

There are 1 best solutions below

2
Josh On

Check your partial classes inherit the base form as well. I just created a .Net 6.0 win forms project and have these four classes working.

BaseForm.designer.cs
namespace WinFormsApp1 {
    partial class BaseForm {
        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing) {
            if (disposing && (components != null)) {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

    #region Windows Form Designer generated code

    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 = "Base Form";
    }

    #endregion
}
}

BaseForm.cs
namespace WinFormsApp1 {
public partial class BaseForm : Form {
    public BaseForm() {
        InitializeComponent();
    }
}
}

Form1.designer.cs
namespace WinFormsApp1 {
partial class Form1 : BaseForm {
    private System.ComponentModel.IContainer components = null;
    protected override void Dispose(bool disposing) {
        if (disposing && (components != null)) {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code
    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
}
}

Form1.cs
namespace WinFormsApp1 {
public partial class Form1 : BaseForm {
    public Form1() {
        InitializeComponent();
    }
}

}

Form 1 Inheriting BaseForm