PC Review


Reply
Thread Tools Rate Thread

Cannot use Mustinherit in a base form

 
 
DavidGB
Guest
Posts: n/a
 
      4th Jun 2009
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

 
Reply With Quote
 
 
 
 
Mick Doherty
Guest
Posts: n/a
 
      4th Jun 2009
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

"DavidGB" <(E-Mail Removed)> wrote in message
news:6AD78F0A-921E-4BC1-A91C-(E-Mail Removed)...
> 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
>

 
Reply With Quote
 
DavidGB
Guest
Posts: n/a
 
      5th Jun 2009
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

"Mick Doherty" wrote:

> 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
>
> "DavidGB" <(E-Mail Removed)> wrote in message
> news:6AD78F0A-921E-4BC1-A91C-(E-Mail Removed)...
> > 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
> >

 
Reply With Quote
 
Mick Doherty
Guest
Posts: n/a
 
      6th Jun 2009
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.



"DavidGB" <(E-Mail Removed)> wrote in message
news:76AF7279-37F0-43AD-91D0-(E-Mail Removed)...
> 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
>


 
Reply With Quote
 
DavidGB
Guest
Posts: n/a
 
      7th Jun 2009
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" wrote:

> 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>
 
Reply With Quote
 
Mick Doherty
Guest
Posts: n/a
 
      8th Jun 2009
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

"DavidGB" <(E-Mail Removed)> wrote in message
news:92C98346-E538-4DCA-8A94-(E-Mail Removed)...
> 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
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MustInherit gives a problem AAaron123 Microsoft VB .NET 5 23rd Mar 2008 01:28 AM
Can not designer- edit form that inherits other abstract (MustInherit) form Joh Smith Microsoft VB .NET 2 11th Oct 2007 12:34 AM
Framework 2.x vs. MustInherit? Arthur Dent Microsoft ASP .NET 2 6th May 2005 03:39 PM
ASP.NET 2 and MustInherit classes? Arthur Dent Microsoft ASP .NET 0 22nd Dec 2004 05:29 PM
MustInherit classes Random Microsoft Dot NET Framework 1 15th Sep 2004 02:27 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:57 PM.