We help IT Professionals succeed at work.

Inheriting from a form

IssacJones
IssacJones asked
on
Hi

I have several forms which have similar properties. For example, the same background image, fonts. Is it possible to inherit from a form so I don't have to reproduce the settings in all new forms?

It is important to note that some of the new forms will have differences eg size and new buttons.

Issac
Comment
Watch Question

Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016
Commented:
yes you can.

the easiest way is to add the Inherits statement just after your form's definition:

Public Class MyNewForm
    Inherits MyBaseForm

Then you may have an error saying that the designer is inheriting from another form, just fix it.
CERTIFIED EXPERT
Top Expert 2015
Commented:
This can be done automatically by Visual Studio. When you add a new Item through the Project menu or otherwise, there is an entry to create an Inherited Form. It changes places in different versions. In Visual Studio 2010, it is in Windows Forms, under the Common Items.

You will be asked from which from you want to inherit, and VS will automatically set everything for you.

You will be able to add anything to the new form: controls, methods, etc. Controls that come from the base form will have a little icon besides them.

If the base form is in the same project, you will also be able to change any property of the controls on the inherited form. But you won't be able to delete a control from the original. In such case, simply move it out of the way and make it invisible in the inherited form.

If the original form is in another project such as a dll used by your application, then you will find out that by default all the properties of the controls are locked in the inherited form. To enable you to modify the control in the inherited form, its Modifier property must be set to Protected in the original when they are not in the same project.

Most of the time, when you make changes to the original, you need to recompile to see the changes trigger down to the inherited forms.

Be careful through all this project. Wait until the original as its final appearance until you inherit from it. If you modify the original later, say you move a Button, you might end up with 2 overlapping controls in the inherited forms.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
When you add a new form to project, in the formname.designer.vb file, you have this

Public Partial Class formname
          Inherits System.Windows.Forms.Form


Change it to

Public Partial Class formname
          Inherits yournamespace.yourbaseform


You may have to open this designer file in notepad.
CERTIFIED EXPERT
Top Expert 2015

Commented:
As you see, there are basically different ways of doing that. But all the discussion about the way to manipulate controls holds, no matter which one you use.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.