Inheriting Windows Forms

Inheriting Windows Forms

Visual Studio .NET 2003

Creating new Windows Forms by inheriting from base forms is a handy way to duplicate your best efforts without going through the process of entirely recreating a form every time you require it.

For more information about inheriting forms at design time using the Inheritance Picker dialog box and how to visually distinguish between security levels of inherited controls, see Inheriting Forms Using the Inheritance Picker Dialog Box.

Note   In order to inherit from a form, the file or namespace containing that form must have been built into an executable file or DLL. To build the project, choose Build from the Build menu. Also, a reference to the namespace must be added to the class inheriting the form.

To inherit a form programmatically

  1. In your class, add a reference to the namespace containing the form you wish to inherit from.
  2. In the class definition, add a reference to the form to inherit from. The reference should include the namespace that contains the form, followed by a period, then the name of the base form itself.
    ' Visual Basic
    Public Class Form2
        Inherits Namespace1.Form1
    // C#
    public class Form2 : Namespace1.Form1
    // C++
    public __gc class Form2 : Namespace1.Form1

When inheriting forms, keep in mind that issues may arise with regard to event handlers being called twice, because each event is being handled by both the base class and the inherited class. For more information on how to avoid this problem, see Troubleshooting Inherited Event Handlers in Visual Basic .NET.

See Also

Inherits Statement | Imports Statement | using (C# keyword) | Effects of Modifying a Base Form's Appearance | Windows Forms Inheritance

© 2015 Microsoft