Cannot use Mustinherit in a base form

D

DavidGB

I have a base form that is used to provide basic graphics, code etc for the
sub forms that the users will see.

In this base form, I want to reference some properties and methods that the
sub forms must implement, so I want to define them as
Public MustOverride Property ...

When I do this, VS 2008 (correctly) complains that the base form must be
declared as MustInherit.

However, if I make it MustInherit, I get all kinds of weird errors, for
example:
A) None of the subforms can be viewed in the designer. It gives the error:
"The designer must create an instance of type 'BaseForm' but it cannot
because the type is declared as abstract"
B) VS starts to complain that several forms have "inconsistent line endings"

Has anyone seen this before, or can anyone suggest what I'm doing wrong?

Thanks
DavidBGB
 
M

Mick Doherty

Create your Abstract Form base:
--8< ------------------------------------------------
Public MustInherit Class CustomFormBase
Inherits Form
Public MustOverride Property MyColorProperty() As Color
End Class
--8< ------------------------------------------------


Create a Form derived from this Abstract form which knows how to implement
all required properties/methods:
--8< ------------------------------------------------
Public Class CustomForm
Inherits CustomFormBase
Private myColor As Color = Color.Red
Public Overrides Property MyColorProperty() As System.Drawing.Color
Get
Return myColor
End Get
Set(ByVal value As System.Drawing.Color)
myColor = value
End Set
End Property
End Class
--8< ------------------------------------------------


Derive your Main Form from this Form and you will have a happy IDE:
--8< ------------------------------------------------
Public Class MainForm
Inherits CustomForm
End Class
--8< ------------------------------------------------

Mick Doherty
http://dotnetrix.co.uk/form.htm
 
D

DavidGB

Thank you for this, but I'm still having trouble!

As soon as I add 'MustInherit' to a form class (to make the Base Class) I
get the error:
"'CustomFormBase' is a type in 'MyProject' and cannot be used as an
expression."

This happpens whether I put it in the regular user code file, or in the
Designer generated one. (Which of them should I be using anyway?)

I don't understand what this error is trying to tell me.

TIA David
 
M

Mick Doherty

Do not add 'MustInherit' or 'MustOverride properties' to a designer
generated Form class.
Add a Class to your project and place the MustInherit Form class there.

You should be using MainForm in the IDE. The other two classes are setting
up the usable Form class.

Add a class to your project.
Replace this code:

--8< ------------------------------------------------
Public Class Class1

End Class
--8< ------------------------------------------------

....with this:

--8< ------------------------------------------------
Public MustInherit Class CustomFormBase
Inherits Form
Public MustOverride Property MyColorProperty() As Color
End Class

Public Class CustomForm
Inherits CustomFormBase
Private myColor As Color = Color.Red
Public Overrides Property MyColorProperty() As System.Drawing.Color
Get
Return myColor
End Get
Set(ByVal value As System.Drawing.Color)
myColor = value
End Set
End Property
End Class
--8< ------------------------------------------------

n.b. you can add these as separate classes but you cannot open the Design
surface of CustomForm, so I prefer to add it as the second class of the
CustomFormBase class as we will not be able to try to open the Designsurface
even accidently. You can use the DesignSurface of CustomFormBase but you
will need to add a Sub New() with InitializeComponent() call in order for
any Controls to be inherited.

Build the project.
Open up the Designer file of your original Form class (i.e.
Form1.Designer.vb) and change the inheritance from:
System.Windows.Form
....to:
CustomForm

Using this example, your Form1 will now have a MyColorProperty which
defaults to Color.Red.
 
D

DavidGB

Thanks, that works. But:

A) I still cannot use the designer on CustomForm - but this is not a problem
B) Any Graphical elements placed in CustomFormBase do not show up in
MainForm (Derived from CustomForm)

So where can I put graphical elements (labels, buttons etc) that are common
to ALL forms?

Also - I thought I had a reasonable understanding of Software Development,
OO, etc, (although not necessarily recent .NET) but I'm sitting out here on
an island in the atlantic without any other developers to discuss things
with. And it's surprisingly difficult.

But Amazon does deliver out here!

Can you suggest a book or two that might help?

Thanks, David

Mick Doherty said:
Do not add 'MustInherit' or 'MustOverride properties' to a designer
generated Form class.
Add a Class to your project and place the MustInherit Form class there.

You should be using MainForm in the IDE. The other two classes are setting
up the usable Form class.

Add a class to your project.
Replace this code:
<Snip>
 
M

Mick Doherty

Hi David,

You can use the Designer on CustomFormBase. CustomForm is a 'go between' and
should not be used for Design. We only created it because the IDE cannot
create an instance of an abstract form and so we need to jump that hurdle.
As stated in a previous response, if you wish to add controls to
CustomFormBase then you need to add a Sub New() with a call to
InitializeComponent().
Any controls you add here will be locked in MainForm. There may be a
workaround for that, but I don't know of it (A custom Designer derived from
DocumentDesigner doesn't appear to accept EnableDesignMode like other
control designers do).

I'm afraid I have no suggestions for books as I don't read them, perhaps
someone else can offer suggestions. I generally use the MSDN library and
Google, or play with code until it works, but since I only program as a
hobby I have no time restrictions. I do find MSDN to be very useful most of
the time.

Regards,

Mick Doherty
http://dotnetrix.co.uk/form.htm
 

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