With Visual Basic .NET, we finally can inherit from one form to another; this capability is called visual inheritance. This means we can create a Windows Forms form; then inherit from that form to create other forms with the same layout, controls, and behaviors. We also can use inheritance to create our own versions of Windows Forms controls. For instance, we might want to create an enhanced TextBox control that performs some specialized validation of the input data. This can be accomplished through inheritance by creating a subclass of the original TextBox control class and enhancing it as needed. Finally, the same capability is available when you work with Web forms.
Inheriting Forms in Code
To inherit a form, you first must add the form you want to use to the project. Then, by changing a single line of code, you can inherit the objects from the existing form. To see how this works, start a new project and create a form that resembles the one in Figure 3.19.
Figure 3.19 Creating a form to use as a template.
Let's create a new form, Form2, which will resemble the one in Figure 3.20.
Figure 3.20 Creating the form that will inherit the objects.
Now that Form2 is created, look at the form's code. You should see the following line at the top of the Public Class Form2:
By changing this line of code to
Form2 also will have the controls from Form1 on it, as shown in Figure 3.21.
Figure 3.21 The second form showing the inherited controls and the original controls.
Notice that Form2 now has both sets of controls. This capability isn't just for the controls on a form. If any of these controls had code associated with them, the code also would have been inherited.
The Inheritance Picker
The Inheritance Picker simplifies the process you just completed by presenting you with two dialog boxes, which help you choose the form to inherit and then change the code for you. To use this feature, select Add Inherited Form from the Project menu to display the dialog box shown in Figure 3.22.
Figure 3.22 The Add New Item dialog box is shown with the Inherited Form option selected.
For this to work, you must use the Build command to compile the form you want to use into either an executable file or a DLL before either that form or DLL can be used by the Inheritance Picker.
On the Inheritance Picker you can specify the name you want to use for the new form. Once you are satisfied, click the Open button to display the dialog box that lists the forms that you can use for inheritance (see Figure 3.23).
Figure 3.23 Selecting a form to inherit.
Choose the form that has the controls you want to use and click OK, or click the Browse button to look for another form in a different application. You can see in Figure 3.24 that the control inherited from the selected form has a little icon in the upper-left corner that signifies that the control is inherited.
You should know that inherited controls can't be moved, nor can the inherited code be changed. These actions can be done only on the original form. It's also important to know that any changes you make to the base form won't be seen in the inherited forms until you rebuild the application.
Figure 3.24 The final form with the inherited controls on it.