views:

204

answers:

6

Theoretically you can derive from a Form but is it something you should not do? I intuitively think so but I've never heard of any rule like this.

Edit: of course I meant some conrete class that already derives from Form. For example if I've got class MyForm : Form, the question is can i derive from MyForm?

+4  A: 

There's no hard and fast rule that prevents you from deriving a windows Form. If you have a good reason to do it (e.g., bolting in some features common throughout your project throughout all forms), then go ahead.

Jon Limjap
+5  A: 

You should derive from Form when creating a new Windows Form. When creating a new form in Visual Studio, the source files you get already derive from Form.

Arjan Einbu
this is obvious and is not what i was asking.
agnieszka
I saw that, and I have upvoted some others here that have other good answers.
Arjan Einbu
Yes it is OK to create your own derived Form class, and use it as a base for creating your other forms.Reasons to do this include: Common look-and-feel/design across forms, bolting on new features like perhaps a dirtyflag for a "Do you wan't to close this without saving?" or an undo stack implementation.
Arjan Einbu
A: 

The question really depends on what your derived class does.

Form and many such end classes are designed to do lots of complex tasks to give you full advantages of form related activities without having to code too much.

The rule would be something like this, "If you intend to do a simple window operation, and if it may not disturb the regular behavior then its better not to derive from form.

Or since form is heavily loaded, you can save memory and cpu time by deriving it from base classes instead of form.

Akash Kava
+2  A: 

It's perfectly reasonable to derive forms from a common base Form-derived class, and can be useful for having a standard look and feel for your application.

Joe
A: 

We've had success deriving a class from form and then deriving all of our forms in the project from it. It allows us to easily apply project-wide policies. All of our forms have a consistent look and feel. It also made it easy to have each form remember its size and location.

NascarEd
+1  A: 

I highly recommend inheriting from a BaseForm. This makes it very easy to f.e. make all EditForms look a like, because you can set the common controls on the base (like buttons), give them a backcolor/image, etc. Same goes for all sort of forms that can be grouped. I usually have 1 BaseForm and then again a BaseForm according to it's 'group' (edit, list, dialog, ...)

It makes you winapp look more consistent.

Same goes for code, usually Edit form have a similar code base: validation, save logic, ... You can put all this logic on the baseform(s) and then have a few abstract methods that you can implement on the childform.

Tommy