7

I have a generic abstract UserControl class, SensorControl, which I want all my sensor control panels to inherit from.

The problem

When attempting to design the EthernetSensorControl (one of my inherited UserControl forms, from within Visual Studio, the following error is displayed in the form designer:

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: DeviceSensorControl --- The base class 'Engine.Sensors.SensorControl' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.

SensorControl class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Engine.Sensors
{
    public abstract class SensorControl<SensorType>
        : UserControl where SensorType : class
    {
        protected SensorType _sensor;
        public SensorControl(SensorType sensor)
        {
            _sensor = sensor;
        }
    }
}

Example inherited class, EthernetSensorControl:

namespace Engine.Sensors
{
    public partial class EthernetSensorControl
        : SensorControl<EthernetSensor>
    {
        public EthernetSensorControl(EthernetSensor sensor)
            : base(sensor)
        {
        }
    }
}

And the call stack:

at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager) at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager) at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager) at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host)

Everything compiles and I can see the panel displayed, but I can't design it. I think the problem may be related to the partial classes. Any ideas?

Petrus Theron
  • 27,855
  • 36
  • 153
  • 287
  • Its worth noting that the correct terminology is a *Generic* class (not a templated class). Although the syntax is similar to C++ templated class, they behave fairly differently. – Justin Oct 14 '10 at 12:55
  • Duly noted. I come from C++. Fixed my question. – Petrus Theron Oct 14 '10 at 12:59

1 Answers1

9

You cannot design a control or form that inherits an abstract class.

(The designer needs to instantiate the base class to serve as the design surface)

The base class also needs to have a parameterless constructor for the designer to call.
This constructor can be private.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • OK. So, I should just add a parameterless constructor to my `SensorControl` class and remove the abstract keyword? – Petrus Theron Oct 14 '10 at 12:53
  • Removed abstract, added (public) parameterless constructor to both `SensorControl` and `DeviceSensorControl` and I still get the same error. – Petrus Theron Oct 14 '10 at 13:02
  • 1
    Related to this perhaps?: http://stackoverflow.com/questions/677609/generic-base-class-for-winform-usercontrol – Petrus Theron Oct 14 '10 at 13:04