About inheritance when I use window forms

T

tony

Hello!

Assume I have three window forms called Frm1, Frm2 and Frm3 each of these
have a method called foo1 and foo2.

Now instead of having this foo1 and foo2 in each of the three window forms I
want to put these foo1 and foo2 in the Base class and then
inherit from this class.

I did the following:
Create a class called Top where I put method foo1 and foo2 then inerit from
Top see below.

When I do this and then try to use forms designer for window form Frm1 I get
into trouble.
I get this error "An error occurred while loading the document. Fix the
error, and then try loading the document again.
The error message follows: The designer could not be shown for this file
because none of the classes within it
can be designed. The designer inspected classes in the file: Frm1 --- The
base class 'Top' could not be loaded.
Ensure the assembly has been referenced or built if it is part of the
project."

So a questiuon is must all window form inherit directly from the Form class?

So if all window form must inherit directly from the Form class how do I
solve my problem
where I want to put some methods in the Base class?

Is the solution that I must keep each method in each window form perhaps as
I had from the beginning.


public class Top : System.Windows.Forms.Form
{
public void foo1()
{}

public void foo2()
{ }
}


public class Frm1 : Top
{
}

//Tony
 
G

Guest

tony

your design is a correct in OO terms and should work at runtime, but for
"visual inheritance" to work at design time you need the usual
InitializeComponent calls. i suggest you create your base form with the
designer first.

"viusal inheritance" is all about achieving a common look for your forms. if
you just want common functionality then try implementing in another way (e.g.
a helper class).

kh
 

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