ne of the coolest features of Visual Basic.NET is inheritance, which provides the ability for one object to inherit another object's methods and properties. Not only does this save coding time initially, but it also saves time later on as changes in the parent object are reflected in all objects that inherit from it. Visual Basic.NET allows you to inherit classes and Windows® Forms. If you're familiar with object-oriented programming (OOP) concepts, then you know what inheritance is. For those of you who are not familiar with OOP, let's forget all the complicated stuff for a minute and just work with inheritance the easy way.
The easiest way to get a handle on inheritance with Windows Forms is to look at an example. The sample application I'll present here simplifies a previously tedious task—namely, creating a consistent UI requiring clones of your interface elements. For instance, you may want your company logo in the top-left corner of each form and certain standard buttons in the upper-right corner of each form. To do this in Visual Basic® 6.0 and earlier, you can create a template and build each form from it, or you can create code that puts the logo and buttons on each form. Whichever approach you take, it will take time and effort to make it work the way you want.
With Visual Basic.NET, this process is simple. You can build the form once, then each succeeding project can inherit from that form, acquiring its interface and its code automatically. If you change the master interface, each project inherits those changes. Windows Forms is the simplest and coolest use of form inheritance.
The sample application is going to mock up part of the interface of an application. The master form will be frmMasterLayout.vb. Let's see what the prototype application I'm going to build looks like. Figure 1 shows two of the forms in the application. Notice how both forms have the same logo and logo placement, and the two buttons in the top-right corner are also the same. All of these features plus the About button on the Start form (on the rearmost form) are part of the master form, which is not shown in Figure 1. Instead of placing the image and buttons on each form, the forms simply inherit the functionality of the master form.
Figure 1Inheritance from the Base Form
So, how can you try this at home? Well, this is where the fun begins because the process is relatively simple.
First, let�s create the master form. In Microsoft .NET parlance, this form is known as the base form because (as you may have guessed) it is the one that will serve as the base for the other forms. In other words, when you create a new form that inherits from the base form, the new form looks and acts just like it.
To create the base form, start Visual Basic.NET. Select Create New Project from the Visual Studio® home page or from the File menu (if you have already started Visual Basic.NET). Click the Visual Basics Projects item in the left pane and select Windows Application in the right pane. Name this form MyProjBaseForm and click OK.
To make this form a base form, you need to change the project type from Windows Application to a class library that your other forms can inherit from. To do this, right-click the project name and select Properties. Select Class Library from the Output type list (see Figure 2) on the General properties page and then click OK.
Now you can add the master form and create its interface. First, delete the default form for the project. Next, right-click the project name in Solution Explorer, hover the mouse over Add, and select Add Windows Form from the shortcut menu. Name this form frmMasterLayout and click Open.
Press F4 to open the form�s properties. Expand the Size property and set the width to 800 and the height to 600.
Next, drag an Image control to the form. Open the properties for the image control and set the Image property to any image on your system. I used the 32X Tech logo. Set the Location X and Y properties to 8.
Next, add a button control to the form and set its Name property to cmdClose and the Text property to Close. Set the Location X and Y properties to 704 and 8, respectively.
Now you need to add two more buttons. Right-click the cmdClose button and select Copy. Right-click the form and select Paste. The new button will be pasted directly on top of the original button. Drag it to a location of 704 and 40, set its Name property to cmdStart, and set the Text property to Start. Change the Visible property to True.
Now, copy the cmdStart button and paste it into the form.
Move the new button to a location of 616 and 8. Set its Name property to cmdAbout and set the Text property to About. Now it�s time to change this button�s behavior. Change the Modifiers property to Protected, which will allow the properties of this control to be changed by any form that inherits the form the control is on. On the other two command buttons, this property is set to Private, effectively locking the property settings on these buttons. Also, make sure the Visible property of this button is False. This will turn off the button unless the inheriting form explicitly turns it on.
To complete the buttons, double-click the Close button to open its Click event procedure. Add this code, which closes the form.
|