Reputation: 7195
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:
AbstractBaseForm.cs
, and add some hello world logic: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";
}
}
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 from AbstractBaseForm.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?
Upvotes: 1
Views: 1750
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