Recipe 3.10. Creating a Form Based on Another Form


Problem

You've created a basic form, and you would like to use it to create other forms that extend the functionality of the basic form.

Solution

Create an inheritance relationship between the original form (the "base" class) and the form with the extended features (the "inherited" class). There are a few different ways to accomplish this, but the easiest is to let Visual Studio establish the inheritance relationship for you through the Inheritance Picker dialog.

Discussion

Use the Project Add Windows Form menu command to add the new form to a new or existing Windows Forms project. When the Add New Item dialog appears, select Inherited Form from the list of templates, type your new forms filename in the Name field, and then click the Add button. As long as there are other forms defined in your application, you will see the Inheritance Picker dialog (Figure 3-4).

Figure 3-4. Visual Studio's Inheritance Picker dialog


To establish the inheritance relationship, select the base form from the list of available forms, and then click the OK button. Visual Studio will add a new form that is derived from the selected base form.

All forms added to your Windows Forms project use inheritance. By default, new forms derive from System.Windows.Forms.Form, but you can indicate another base form from your own project. If you look in the "designer" file associated with the form, you will see the following statements in standard forms:

 Partial Class Form2    Inherits System.Windows.Forms.Form 

When you alter the base class through the Inheritance Picker, these statements change to reflect the selected base form:

 Partial Class Form2    Inherits WindowsApplication1.Form1 

(A form's designer file is hidden by default. Click on the Show All Files button in the Solution Explorer, and then expand the branch for a form to see its designer file.)

You can manually establish the inheritance relationship by modifying the Inherits statement yourself to include the correct base class.

Visual Studio must be able to create an instance of the base form before it can show you the derived form through the Form Designer (or even list the form in the Inheritance Picker). This requires that a compiled version of that base form exists. Before using the Inheritance Picker to establish form relationships, build your project using the Build Build WindowsApplication1 (or similar) menu command.

See Also

Although it's not covered in a separate recipe in this chapter, creating inherited user controls follows the same process. Select Inherited User Control in the Add New Item dialog's template list to establish such a relationship.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net