By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 775 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Yasutaka Ito" <no****@nonexistent.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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

Jul 21 '05 #2

P: n/a
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

"Yasutaka Ito" <no****@nonexistent.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.