Previous Section  < Day Day Up >  Next Section

6.6. Forms Inheritance

Just as a class inherits from a base class, a GUI form梬hich is also a class梒an inherit the settings, properties, and control layout of a preexisting form. This means that you can create forms with standard features to serve as templates for derived forms. Before looking at the details of inheritance, let's first examine how to store a set of base forms in a code library and organize them by namespace.

Building and Using a Forms Library

Each form consists of a physical .cs file. A library of multiple forms is created by compiling each .cs file into a common .dll file. After this is done, the forms can be accessed by any compliant language梟ot just the one they are written in.

As an example, let's use the compiler from the command line to compile two forms into a single .dll file:

csc /t:library product.cs customer.cs  /out:ADCFormLib.dll

A base form must provide a namespace for the derived form to reference it. The following code defines a Products namespace for our example:

namespace Products


   public class ProductForm : System.Windows.Forms.Form


To inherit this form, a class uses the standard inheritance syntax and designates the base class by its namespace and class name:

// User Form derived from base class Form

class NewProductForm: Products.ProductForm


As a final step, the compiler must be given a reference to the external assembly ADCFormLib so that the base class can be located. If using VS.NET, you use the Project-AddReference menu option to specify the assembly; from the command line, the reference flag is used.

csc /t:winexe /r:ADCFormLib.dll myApp.cs

Using the Inherited Form

If the derived form provides no additional code, it generates a form identical to its base form when executed. Of course, the derived form is free to add controls and supporting code. The only restriction is that menu items cannot be added to an existing menu; however, an entire menu can be added to the form and even replace an existing one on the base form.

The properties of inherited controls can be changed, but their default access modifier of private must first be changed to protected, and the base form then recompiled. The derived form is then free to make modifications: It may reposition the control or even set its Visible property to false to keep it from being displayed.

Overriding Events

Suppose the base form contains a button that responds to a click by calling event handler code to close the form. However, in your derived form, you want to add some data verification checks before the form closes. One's instinct is to add a delegate and event handler method to respond to the button Click event in the derived form. However, this does not override the original event handler in the base form, and both event handling routines get called. The solution is to restructure the original event handler to call a virtual method that can be overridden in the derived form. Here is sample code for the base form:

private void btn1_Clicked(object sender, System.EventArgs e)


   ButtonClicks();   // Have virtual method do actual work


protected virtual void ButtonClicks()




The derived form simply overrides the virtual method and includes its own code to handle the event:

protected override void ButtonClicks() {

   // Code to perform any data validation



Creating Inherited Forms with Visual Studio.NET

To create an inherited form using Visual Studio, open a project containing the form to serve as the base and compile it. Then, select Project-Add Inherited Form from the menu. Give the new form a name and open it. Next, an Inheritance Picker dialog box appears that lists the eligible base forms. Use the Browse feature to display forms that are in external libraries.

Select a base form and the new inherited form appears in the designer window. All of the controls on the form are marked with a plus (+), and you will find that only properties on the form itself are exposed. At this point, you can add controls or modify existing ones using the techniques just described.

    Previous Section  < Day Day Up >  Next Section