15.13 Visual Inheritance

Chapter 11 discussed how to create classes via inheritance. We’ve also used inheritance to create Forms that display a GUI, by deriving our new Form classes from class System.Windows.Forms.Form. 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 aspects—such as sizing, component layout, spacing between GUI components, colors and fonts.

Visual inheritance enables you to achieve visual consistency. 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 app for uniformity and branding. You also can create controls that inherit from other controls. For example, you might create a custom UserControl (discussed in Section 15.14) that’s derived from an existing control.

Creating a Base Form

Class VisualInheritanceBaseForm (Fig. 15.45) derives from Form. The output depicts how the Form works. The GUI contains two Labels with text Bugs, Bugs, Bugs and Copyright 2017, by Deitel & Associates, Inc., as well as one Button displaying the text Learn More. When a user presses the Learn More Button, method learnMoreButton_Click (lines 18–24) is invoked. This method displays a MessageBox that provides some informative text.

Fig. 15.45 Base Form for use with visual inheritance.

Alternate View

  1    // Fig. 15.45: VisualInheritanceBaseForm.cs
  2    // Base Form for use with visual inheritance.
  3    using System;
  4    using System.Windows.Forms;
  5
  6    namespace VisualInheritanceBase
  7    {
  8       // base Form used to demonstrate visual inheritance
  9       public partial class VisualInheritanceBaseForm : Form
  10      {
  11          // constructor
  12          public VisualInheritanceBaseForm()
  13          {
  14             InitializeComponent();
  15          }
  16
  17          // display MessageBox when Button is clicked
  18          private void learnMoreButton_Click(object sender, EventArgs e)
  19          {
  20              MessageBox.Show(
  21                 "Bugs, Bugs, Bugs is a product of deitel.com",
  22                 "Learn More", MessageBoxButtons.OK,
  23                 MessageBoxIcon.Information);
  24          }
  25       }
  26    }

Steps for Declaring and Using a Reusable Class

Before a Form (or any class) can be used in multiple apps, it must be placed in a class library to make it reusable. The steps for creating a reusable class are:

  1. Declare a public class. If the class is not public, it can be used only by other classes in the same assembly—that is, compiled into the same DLL or EXE file.

  2. Choose a namespace name and add a namespace declaration to the source-code file for the reusable class declaration.

  3. Compile the class into a class library.

  4. Add a reference to the class library in an app.

  5. Use the class.

Let’s take a look at these steps in the context of this example:

Step 1: Creating a public Class

For Step 1 in this discussion, we use the public class VisualInheritanceBaseForm declared in Fig. 15.45. By default, every new Form class you create is declared as a public class.

Step 2: Adding the namespace Declaration

For Step 2, we use the namespace declaration that was created for us by the IDE. By default, every new class you define is placed in a namespace with the same name as the project. In almost every example in the text, we’ve seen that classes from preexisting libraries, such as the .NET Framework Class Library, can be imported into a C# app. Each class belongs to a namespace that contains a group of related classes. As apps become more complex, namespaces help you manage the complexity of app components. Class libraries and namespaces also facilitate software reuse by enabling apps to add classes from other namespaces (as we’ve done in many examples). We removed the namespace declarations in earlier chapters because they were not necessary.

Placing a class inside a namespace declaration indicates that the class is part of the specified namespace. The namespace name is part of the fully qualified class name, so the name of class VisualInheritanceTestForm is actually VisualInheritanceBase.Visual-InheritanceBaseForm. You can use this fully qualified name in your apps, or you can write a using directive and use the class’s simple name (the unqualified class name—VisualInheritanceBaseForm) in the app. If another namespace also contains a class with the same name, the fully qualified class names can be used to distinguish between the classes in the app and prevent a name conflict (also called a name collision).

Step 3: Compiling the Class Library

To allow other Forms to inherit from VisualInheritanceForm, we must package Visual-InheritanceForm as a class library and compile it into a .dll file. Such as file is known as a dynamically linked library—a way to package classes that you can reference from other apps. Right click the project name in the Solution Explorer and select Properties, then choose the Application tab. In the Output type drop-down list, change Windows Application to Class Library. Building the project produces the .dll. You can configure a project to be a class library when you first create it by selecting the Class Library template in the New Project dialog. [Note: A class library cannot execute as a stand-alone app. The screen captures in Fig. 15.45 were taken before changing the project to a class library.]

Step 4: Adding a Reference to the Class Library

Once the class is compiled and stored in the class library file, the library can be referenced from any app by indicating to Visual Studio where to find the class library file. To visually inherit from VisualInheritanceBaseForm, first create a new Windows app. Right click the References node in the Solution Explorer window and select Add Reference... from the pop-up menu that appears. The dialog box that appears will contain a list of class libraries from the .NET Framework. Some class libraries, like the one containing the System namespace, are so common that they’re added to your app by the IDE. The ones in this list are not.

In the Reference Manager dialog box, click Browse then click the Browse… button. When you build a class library, Visual C# places the .dll file in the project’s binDebug or binRelease folder, depending on whether the Solution Configurations drop-down list in the IDE’s toolbar is set to Debug or Release. In the Browse tab, you can navigate to the directory containing the class library file you created in Step 3, as shown in Fig. 15.46. Select the .dll file and click Add.


Fig. 15.46 Using the Reference Manager dialog to browse for a DLL.

Step 5: Using the Class—Deriving from a Base Form

Open the file that defines the new app’s GUI and modify the line that defines the class to indicate that the app’s Form should inherit from class VisualInheritanceBaseForm. The class-declaration line should now appear as follows:


public partial class VisualInheritanceTestForm :
   VisualInheritanceBase.VisualInheritanceBaseForm

Unless you specify namespace VisualInheritanceBase in a using directive, you must use the fully qualified name VisualInheritanceBase.VisualInheritanceBaseForm. In Design view, the new app’s Form should now display the controls inherited from the base Form (Fig. 15.47). We can now add more components to the Form.

Fig. 15.47 Form demonstrating visual inheritance.

Class VisualInheritanceTestForm

Class VisualInheritanceTestForm (Fig. 15.48) is a derived class of VisualInheritanceBaseForm. The output illustrates the functionality of the program. The components, their layouts and the functionality of base class VisualInheritanceBaseForm (Fig. 15.45) are inherited by VisualInheritanceTestForm. We added an additional Button with text About this Program. When a user presses this Button, method aboutButton_Click (Fig. 15.48, lines 19–25) is invoked. This method displays another MessageBox providing different informative text (lines 21–24).

Fig. 15.48 Derived Form using visual inheritance.

Alternate View

  1    // Fig. 15.48: VisualInheritanceTestForm.cs
  2    // Derived Form using visual inheritance.
  3    using System;
  4    using System.Windows.Forms;
  5
  6    namespace VisualInheritanceTest
  7    {
  8       // derived form using visual inheritance
  9       public partial class VisualInheritanceTestForm :
 10          VisualInheritanceBase.VisualInheritanceBaseForm
 11       {
 12          // constructor
 13          public VisualInheritanceTestForm()
 14          {
 15             InitializeComponent();
 16          }
 17
 18          // display MessageBox when Button is clicked
 19          private void aboutButton_Click(object sender, EventArgs e)
 20          {
 21             MessageBox.Show(
 22                "This program was created by Deitel & Associates.",
 23                "About This Program", MessageBoxButtons.OK,
 24                MessageBoxIcon.Information);
 25          }
 26       }
 27    }

If a user clicks the Learn More button, the event is handled by the base-class event handler learnMoreButton_Click. Because VisualInheritanceBaseForm uses a private access modifier to declare its controls, VisualInheritanceTestForm cannot modify the controls inherited from class VisualInheritanceBaseForm visually or programmatically. The IDE displays a small icon at the top left of the visually inherited controls to indicate that they’re inherited and cannot be altered.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset