Udostępnij za pośrednictwem


How to: Inherit Windows Forms

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. The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

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.

    Public Class Form2
        Inherits Namespace1.Form1
    
    public 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

Concepts

Effects of Modifying a Base Form's Appearance

Reference

Inheritance Picker Dialog Box

Inherits Statement

Imports Statement (.NET Namespace and Type)

using (C# Reference)

Other Resources

Windows Forms Visual Inheritance