Chapter 11 discussed how to create classes via inheritance. We’ve also used inheritance to create Form
s 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.
Form
Class VisualInheritanceBaseForm
(Fig. 15.45) derives from Form
. The output depicts how the Form
works. The GUI contains two Label
s 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.
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:
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.
Choose a namespace name and add a namespace
declaration to the source-code file for the reusable class declaration.
Compile the class into a class library.
Add a reference to the class library in an app.
Use the class.
Let’s take a look at these steps in the context of this example:
public
ClassFor 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.
namespace
DeclarationFor 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).
To allow other Form
s 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.]
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.
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
.
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).
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.