sapbucket
sapbucket

Reputation: 7195

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

I am following the solution provided by Juan Carlos Diaz here

My problem is that I do not see any of the abstract class's form controls displayed in the concrete class. I am expecting them to be there so that I may use the design editor with the concrete class.

Here are the steps that I took:

  1. create a new solution
  2. create a new winforms project (.Net 4.5.2)
  3. create a Form titled AbstractBaseForm.cs, and add some hello world logic:

enter image description here

  1. add abstract keyword to AbstractBaseForm.cs; your code should look something like this:

    public abstract partial class AbstractBaseForm : Form
    {
        protected AbstractBaseForm()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "Hello World";
        }
    }
    
  2. Add the following class to your project:

    public class AbstractControlDescriptionProvider<TAbstract, TBase> : TypeDescriptionProvider
    {
        public AbstractControlDescriptionProvider()
            : base(TypeDescriptor.GetProvider(typeof (TAbstract)))
        {
        }
    
        public override Type GetReflectionType(Type objectType, object instance)
        {
            if (objectType == typeof (TAbstract))
                return typeof (TBase);
    
            return base.GetReflectionType(objectType, instance);
        }
    
        public override object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
        {
            if (objectType == typeof (TAbstract))
                objectType = typeof (TBase);
    
            return base.CreateInstance(provider, objectType, argTypes, args);
        }
    }
    
  3. Add the following attribute to AbstractBaseForm.cs:

    [TypeDescriptionProvider(typeof (AbstractControlDescriptionProvider<AbstractBaseForm, Form>))]
    public abstract partial class AbstractBaseForm : Form
    {
    
  4. Add a second Form to the project, titled ConcreteForm.cs and have it inherit from AbstractBaseForm.cs, like this:

    public partial class ConcreteForm : AbstractBaseForm
    {
        public ConcreteForm()
        {
             InitializeComponent();
        }
    }
    
  5. Change program.cs so that it loads ConcreteForm.cs like this:

    Application.Run(new ConcreteForm());
    
  6. Execute the project. You should see ConcreteForm.cs load, and clicking the button changes the label to say "Hello World".

  7. Close the application, and click on ConcreteForm.cs, bringing up its design view. You will see this:

enter image description here

Why do I not see the inherited controls from AbstractBaseForm.cs in the design view?

Upvotes: 1

Views: 1750

Answers (1)

benPearce
benPearce

Reputation: 38333

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.

Upvotes: 2

Related Questions