How to Get Visual Studio 2008 Windows Forms Designer to Render a Form That Implements an Abstract Base Class

How can I get Visual Studio 2008 Windows Forms designer to render a Form that implements an abstract base class?

I KNEW there had to be a way to do this (and I found a way to do this cleanly). Sheng's solution is exactly what I came up with as a temporary workaround but after a friend pointed out that the Form class eventually inherited from an abstract class, we SHOULD be able to get this done. If they can do it, we can do it.

We went from this code to the problem

Form1 : Form

Problem

public class Form1 : BaseForm
...
public abstract class BaseForm : Form

This is where the initial question came into play. As said before, a friend pointed out that System.Windows.Forms.Form implements a base class that is abstract. We were able to find...

Proof of a better solution

  • Inheritance Hierarchy:

    • System.Object
      • System.MarshalByRefObject (public **abstract** class MarshalByRefObject)
        • System.ComponentModel.Component
          • System.Windows.Forms.Control
            • System.Windows.Forms.ScrollableControl
              • System.Windows.Forms.ContainerControl
                • System.Windows.Forms.Form

From this, we knew that it was possible for the designer to show a class that implemented a base abstract class, it just couldn't show a designer class that immediately implemented a base abstract class. There had to be at max 5 inbetween, but we tested 1 layer of abstraction and initially came up with this solution.

Initial Solution

public class Form1 : MiddleClass
...
public class MiddleClass : BaseForm
...
public abstract class BaseForm : Form
...

This actually works and the designer renders it fine, problem solved.... except you have an extra level of inheritance in your production application that was only necessary because of an inadequacy in the winforms designer!

This isn't a 100% surefire solution but its pretty good. Basically you use #if DEBUG to come up with the refined solution.

Refined Solution

Form1.cs

public class Form1
#if DEBUG
: MiddleClass
#else
: BaseForm
#endif
...

MiddleClass.cs

public class MiddleClass : BaseForm
...

BaseForm.cs

public abstract class BaseForm : Form
...

What this does is only use the solution outlined in "initial solution", if it is in debug mode. The idea is that you will never release production mode via a debug build and that you will always design in debug mode.

The designer will always run against the code built in the current mode, so you cannot use the designer in release mode. However, as long as you design in debug mode and release the code built in release mode, you are good to go.

The only surefire solution would be if you can test for design mode via a preprocessor directive.

How to fix the errror 'The designer must create an instance of type but cant because it marked abstract

The designer doesn't have any problem in showing an abstract control in designer. The problem is when your control has an abstract base class.

Let's say you have an abstract BaseControl as base class for MyControl. Then when you try to see BaseControl in designer, there is no problem, but the designer cannot show MyControl.

The problem is because when you open MyControl in design view, the designer tries to create an instance of the base class to show it in the designer but since the base class is abstract it fails to create an instance and fails to load.

As an option to solve the problem, you can create a non-abstract base class deriving from the base control for debug mode. Then the designer can show MyControl.

Note: Using #if DEBUG is just to get rid of the intermediate non-abstract base when you build for RELEASE. If you don't care about it, you don't need these directives and you can just create the intermediate non abstract base and use it.

namespace SampleWinApp
{
#if DEBUG
public partial class MyControl : NonAbstractBase
#else
public partial class MyControl : BaseControl
#endif
{
public MyControl()
{
InitializeComponent();
}
}
#if DEBUG
public class NonAbstractBase : BaseControl { }
#endif
}

And here is my abstract BaseControl:

namespace SampleWinApp
{
public abstract partial class BaseControl : UserControl
{
public BaseControl()
{
InitializeComponent();
}
}
}

C# custom Windows Forms base class and designer

You should add

<SubType>Component</SubType>

to the project file entry of your base class.

Controls from abstract base-Form not shown when inherited by a child Form

Add a call to the base constructor

public partial class ConcreteForm : AbstractBaseForm
{
public ConcreteForm() : base()
{
InitializeComponent();
}
}

And as @hans-passant suggests, remove the abstract keyword. With the abstract keyword in place I am receiving the following error:

The designer must create an instance of type
'WindowsFormsApplication1.Form1' but it cannot because the type is
declared as abstract.



Related Topics



Leave a reply



Submit