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.
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) |
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
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:
Public Overrides Sub OverrideMethod( )
MsgBox("Derived OverrideMethod")
MyBase.OverrideMethod
End Sub
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:
- Private
The control can be modified only in the base form - Protected
The control can be modified only by the deriving form - Public
The control can be modified only by any form or code module - Friend
The 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.
• Table of Contents
• Index
Sams Teach Yourself Visual Studio® .NET 2003 in 21 Days
By
Jason Beres
Publisher
: Sams Publishing
Pub Date
: January 14, 2003
ISBN
: 0-672-32421-0
Pages
: 696
Sams Teach Yourself Visual Studio .NET in 21 Days will help developers that are new to application development and experienced developers understand how to use the .NET Framework and Visual Studio .NET to rapidly develop any type of computer application. The Visual Studio .NET development environment is the most comprehensive developer tool ever created, putting that together with the .NET Frameworks' Class Libraries, the developer has everything he or she needs to get up-to-speed on Microsoft's latest revolution in application development. This book will guide the developer through using the VS .NET IDE, the Visual Basic .NET and C# language, and the supporting tools available from Microsoft to create Windows and Web-based applications. The market is full of books that pretty much say the same thing, which is already available in the help files, the author of this book has written and deployed over a dozen successful applications using Visual Studio .NET and the .NET Framework. All of his expertise and experience is used to give you the most comprehensive title on using Visual Studio .NET.