Visual Basic 2005 for Programmers (2nd Edition)

14.13. Visual Inheritance

Chapter 10 discussed how to create classes by inheriting from other classes. We have also used inheritance to create Forms that display a GUI, by deriving our new Form classes from class System.Windows.Forms.Form (the code that indicates the inheritance relationship appears in the Form's Designer.vb file). This is an example of visual inheritance. The derived Form class contains the functionality of its Form base class, including any base-class properties, methods, variables and controls. The derived class also inherits all visual aspectssuch as size, component layout, spacing between GUI components, colors and fontsfrom its base class.

Visual inheritance enables you to achieve visual consistency across your applications by giving them a common look-and-feel. This also reduces the learning curve as your users move between applications. For example, you could define a base Form that contains a product's logo, a specific background color, a predefined menu bar and other elements. You then could use the base Form throughout an application for uniformity and branding.

Class FrmVisualInheritance (Fig. 14.45) derives from Form. The output depicts the workings of the Form. The GUI contains two Labels with the text Bugs, Bugs, Bugs and Copyright 2006, by Deitel & Associates, Inc., as well as one Button displaying the text Learn More. When a user presses the Learn More Button, the event handler btnLearnMore_Click (lines 512) displays a MessageBox that provides some informative text.

Figure 14.45. Class FrmVisualInheritance, which inherits from class Form, contains a Button (Learn More).

1 ' Fig. 14.45: FrmVisualInheritance.vb 2 ' Base Form for use with visual inheritance. 3 Public Class FrmVisualInheritance 4 ' display MessageBox when Button is clicked 5 Private Sub btnLearnMore_Click(ByVal sender As System.Object, _ 6 ByVal e As System.EventArgs) Handles btnLearnMore.Click 7 8 MessageBox.Show( _ 9 "Bugs, Bugs, Bugs is a product of deitel.com", _ 10 "Learn More", MessageBoxButtons.OK, _ 11 MessageBoxIcon.Information) 12 End Sub ' btnLearnMore_Click 13 End Class ' FrmVisualInheritance

To allow other Forms to inherit from FrmVisualInheritance, we must package it as a class library in a .dll file. To do so, right click the project name in the Solution Explorer and select Properties, then choose the Application tab. In the Application type drop-down list, change Windows Application to Class Library. Building the project produces the .dll. The name of the solution that contains FrmVisualInheritance becomes parts of the class's fully qualified namein this case, VisualInheritance.FrmVisualInheritance.

To visually inherit from FrmVisualInheritance, first create a new Windows application. In this application, add a reference to the .dll you just created (located in the bin/Release folder of the solution containing Fig. 14.45). Then open the Designer.vb file for the new application's Form and modify the line

Inherits System.Windows.Forms.Form

to indicate that the application's Form should inherit from class FrmVisualInheritance instead. The Inherits line in the Designer.vb file should now appear as follows:

Inherits VisualInheritance.FrmVisualInheritance

Note that you must either specify FrmVisualInheritance's fully qualified name or use an Imports declaration to indicate that the new application uses classes from the namespace VisualInheritance. In Design view, the new application's Form now displays the inherited controls of the base class FrmVisualInheritance (as shown in Fig. 14.46). You can now add more components to the Form.

Figure 14.46. Form demonstrating visual inheritance.

Class FrmVisualInheritanceTest (Fig. 14.47) derives from FrmVisualInheritance (Fig. 14.45). The GUI contains the components inherited from FrmVisualInheritance and a Button with the text About this Program. When the user presses this Button, the event handler btnAbout_Click (lines 512) displays another MessageBox providing different informative text.

Figure 14.47. Class FrmVisualInheritanceTest, which inherits from class FrmVisualInheritance, contains an additional Button.

1 ' Fig. 14.47: FrmVisualInheritanceTest.vb 2 ' Derived Form using visual inheritance. 3 Public Class FrmVisualInheritanceTest 4 ' display MessageBox when Button is clicked 5 Private Sub btnAbout_Click(ByVal sender As System.Object, _ 6 ByVal e As System.EventArgs) Handles btnAbout.Click 7 8 MessageBox.Show( _ 9 "This program was created by Deitel & Associates", _ 10 "About this Program", MessageBoxButtons.OK, _ 11 MessageBoxIcon.Information) 12 End Sub ' btnAbout_Click 13 End Class ' FrmVisualInheritanceTest

Figure 14.47 demonstrates that the components, their layouts and the functionality of base-class FrmVisualInheritance (Fig. 14.45) are inherited by FrmVisualInheritanceTest. If a user clicks the button Learn More, the base class event handler btnLearnMore_Click displays a MessageBox. FrmVisualInheritance uses a Private access modifier to declare its controls (in its Designer.vb file), so class FrmVisualInheritanceTest cannot modify the inherited controls.

Категории