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:
- create a new solution
- create a new winforms project (.Net 4.5.2)
- create a Form titled
AbstractBaseForm.cs
, and add some hello world logic:
add
abstract
keyword toAbstractBaseForm.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"; } }
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); } }
Add the following attribute to
AbstractBaseForm.cs
:[TypeDescriptionProvider(typeof (AbstractControlDescriptionProvider<AbstractBaseForm, Form>))] public abstract partial class AbstractBaseForm : Form {
Add a second Form to the project, titled
ConcreteForm.cs
and have it inherit fromAbstractBaseForm.cs
, like this:public partial class ConcreteForm : AbstractBaseForm { public ConcreteForm() { InitializeComponent(); } }
Change program.cs so that it loads
ConcreteForm.cs
like this:Application.Run(new ConcreteForm());
Execute the project. You should see
ConcreteForm.cs
load, and clicking the button changes the label to say "Hello World".Close the application, and click on
ConcreteForm.cs
, bringing up its design view. You will see this:
Why do I not see the inherited controls from AbstractBaseForm.cs
in the design view?