Using Inheritance in Windows Forms
If you're a Visual Basic 6 programmer, inheritance is a very exciting new feature that can save you many hours of programming. Inheritance enables you to define classes that serve as the basis for derived classes.
Derived classes inherit a base class's functionality and can extend the properties, events, and methods of the base class. They can also override inherited methods with new implementations. All classes created with Visual Studio .NET are inheritable by default. Because the forms you design are really classes, you can use inheritance to define new forms based on existing ones.
A few good examples of inheriting forms are
Data entry pages could be inherited to be used with different data sources, but keeping the same user interface
Dialog boxes having a common look and feel, with colors and button locations predefined
Learning how inheritance works with Windows Forms is a great way to learn about inheritance for all classes. As you now realize, a form is just a class, and it inherits from a base class called System.Windows.Forms.Form. This base class provides the functionality you need to design Windows Forms applications. The same concepts are used in all designable objects in Visual Studio .NET.
Understanding Inheritance
It's important to understand that a derived class can override an inherited property, event, or method of a base class. The keywords used to define a method indicate this behavior. These keywords are listed in Table 3.5.
Table 3.5 Properties Used for Inheriting Objects
Property |
Description |
Overridable |
Can be overridden |
MustOverride |
Must be overridden in derived class |
Overrides |
Replaces method from base (inherited) class |
NotOverridable |
Can't be overridden (Default) |
In the following Visual Basic .NET code snippet, there are two classes. BaseClass has an overridable method called OverrideMethod. The very first line of the derived class indicates that it inherits from the base class, so there's an OverrideMethod that overrides the base class's OverrideMethod.
VB.NET
Public Class BaseClass Public Overridable Sub OverrideMethod( ) MsgBox("Base OverrideMethod") End Sub End Class Public Class DerivedClass Inherits BaseClass Public Overrides Sub OverrideMethod( ) MsgBox("Derived OverrideMethod") End Sub End Class
C#
public class BaseClass { public virtual void OverrideMethod() { Messagebox.Show("Base OverrideMethod"); } } public class DerivedClass : BaseClass { public override void OverrideMethod() { Messagebox.Show("Derived OverrideMethod"); } }
There might be situations in which you don't want to completely overwrite the base class's method, but simply want to add some functionality to it. To do so, just add MyBase.MethodName, as the following code snippet demonstrates:
VB.NET
Public Overrides Sub OverrideMethod( ) MsgBox("Derived OverrideMethod") MyBase.OverrideMethod End Sub
C#
public override void OverrideMethod() { Messagebox.Show("Derived OverrideMethod"); base.OverrideMethod(); }
In Visual Basic .NET, the MyBase keyword specified the current base class; in C#, the base keyword specifies the base class. Inheritance is everywhere in .NET, and by the end of this book, you'll have a good understanding how all the object-oriented techniques are used in .NET.
Implementing Inheritance with Visual Studio .NET
Every control in the Toolbox has a Modifiers property. The Modifier specifies the accessibility of the control in the derived forms. For example, when you set the modifier to Public, you're indicating that properties of this control can be modified in an inherited form. That means you have complete control over each object on a form, and you can specify what inheritance capabilities can be used. The following list defines each Modifier:
PrivateThe control can be modified only in the base form
ProtectedThe control can be modified only by the deriving form
PublicThe control can be modified only by any form or code module
FriendThe control can be modified only within the base form's project
To see how this works, you're going to add a new form to the HelloNET application, and then inherit that form. To start, right-click the HelloNET project name in the Solution Explorer and select Add, Add Windows Form. Change the Name to BaseForm, and click the OK button to add it to your solution.
On the BaseForm in the Windows Forms Designer, drag a few TextBox controls and Button controls from the Toolbox to the form. In the Properties window, change the Modifier for one of the controls on your form to Public. Leave the remaining controls properties the same.
From the Build Menu, select Build Solution to make sure that the forms are saved and the controls are up to date. Now you need to add a new form to your application. But instead of adding a Windows Form, you're going to right-click the project name in the Solution Explorer, and select Add, Add Inherited Form. This brings up the Add New Item dialog as if you were adding a regular form.
In the Add New Item dialog, change the Name to InheritedForm, and click the OK button. Now the Inheritance Picker dialog pops up, as shown in Figure 3.15.
Figure 3.15 The Inheritance Picker dialog box.
From the Inheritance Picker, select the BaseForm form and click the OK button. You'll notice that every form in your solution is also listed. Because a form is a class, and every class can be inherited, you can inherit any form.
After the InheritedForm is added to the solution, notice that you can't double-click the controls that aren't set to Public. Notice also that all the properties for the controls are disabled in the Properties window. By setting modifiers on controls, and making their properties overridable, you can implement a very robust inheritance mechanism for your Windows Forms solutions.