Cannot open derived form in design view if the base form is marked 'abstract'.

Y

Yasutaka Ito

Hi folks!

I have a BaseForm class that inherits System.Windows.Forms.Form. It has a
property, whose value I need supplied by the class that inherits it. The
BaseForm usees the value supplied into this property in its Load event.

So, I gave the BaseForm and the property 'abstract' modifier, and put the
implementation of the property in the inherited class; say MyForm. However,
when I did this, I no longer can open MyForm in the design view. I'm stuck
if I can't visually design MyForm.... :(

Would appreciate any input, even if it means altogether different way to
accomplish.

thanks!
-Yasutaka
 
H

Herfried K. Wagner [MVP]

* "Yasutaka Ito said:
I have a BaseForm class that inherits System.Windows.Forms.Form. It has a
property, whose value I need supplied by the class that inherits it. The
BaseForm usees the value supplied into this property in its Load event.

So, I gave the BaseForm and the property 'abstract' modifier, and put the
implementation of the property in the inherited class; say MyForm. However,
when I did this, I no longer can open MyForm in the design view. I'm stuck
if I can't visually design MyForm.... :(

You will have to remove the 'abstact' in order to be able to work with
the designer. That's a "limitation" of the designer.
 
M

Miha Markic

Yes, designer won't work with abstract forms.
You should avoid use of abstract keyword (use virtual instead) if you want
to use visual designer.
 
1

100

Hi Yasutaka Ito,

Yes, you cannot use the deisgner because it cannot create objects of
abstract class.
So, if you want to use the designer what you might want to do is not do
declare the BaseForm class as abstract. Declare the property as virtual
instead of abstract.

Of course you cannot force the inheritors to provide meaningful
implemetation of the property. To workarrond this you can make your default
implemetation to throw and exception. To prevent the base class to be
instantiated make its constructor protected. My tests show it won't mess up
with the designer. If it does leave it public. In your case base calss
cannot be instantiated because it uses Prop in OnLoad and it throws
exception. But the tesigner cannot istantiate it either ;) so we are back to
square one. So, in OnLoad we have to check whether the form is in design
mode or in run-time. This can be check by reading the form's protected
property *DesignMode* and if it returns true - don't read the proerty use
some default value. This check can be done in the property's get accessor,
but since it might can be checked frequently and the Load is called only
once when the form is loading I believe you can save one *if* intruction by
check DesignMode in OnLoad.
like this
class BaseForm: Form
{
.....
public virtual string Prop
{
get
{
throw new NotImplementedException();
}
}

protected BaseForm()
{
}
....
protected override void OnLoad(System.EventArgs e)
{
if(!this.DesignMode)
//read Prop and do something
else
//Use some default value
}
}

This way an exception will be thrown as soon as Prop is used and is not
overriden by an inheritor.
Bare in mind tha the designer instantiate an object (form) of the base class
of the form you actually design. That means that in design mode you cannot
use the Prop's values returned by teh inheritors you can use only some
default value.

HTH
B\rgds
100
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top