Visual Basic has traditionally been known as a language in which user interfaces (UI) could be developed quickly. But it has also been known by C++ and Java developers as a language crippled by its lack of support for object-oriented (OO) features, such as inheritance. Enter VB.NET, which shipped almost a year ago, sporting full OO language capabilities. Visual Basic developers now have the right to hold their heads high—provided they learn to leverage object-oriented features. To get started putting these features to work, let's look at how inheritance can improve upon UI development. To demonstrate, we'll work through an example.
Setting the stage
Imagine that a fictitious company, Carmona Technologies, has built a VB 6.0 application with 100-plus forms. The marketing department wanted the application to look consistent, so each form was designed to carry the company’s logo in the bottom-left corner. Now, let's say that another company buys out Carmona Technologies and wants its logo to appear in the top-right corner of the form. The developers must now expend considerable effort to change each form individually.
But if the application had been written in any OO language, such as VB.NET or C#.NET, this effort wouldn't be necessary. Since both of these languages support inheritance, the developers would be able to create a base form containing the logo. With the logo in the base form, they could change its position in a single location, and the change would cascade to the derived forms.
Creating the base form
The base form should typically be located in a class library project. This allows you to use this base form from multiple applications. For demonstration purposes, we'll create a VB.NET class library project named BaseClasses. In this library, we'll add a new form called BaseForm. VB developers might not be used to the idea that a class library can contain a form. If you have difficulty understanding this, remember that a form is just a class. On this form, we will add the company logo in the bottom-left corner, as shown in Figure A.
To add the logo, we'll use a picture box. Set the Image property of the picture box to the path of the logo file. We also need to position the picture box so that the logo appears where we want. By default, the control is anchored to the top and left, so we'll need to change it to the bottom and left. Try resizing the form, and you'll see that the picture box always appears in the bottom-left corner regardless of the size of the form.
When a form inherits from a base form, the controls on the base form appear in the derived form. The properties of these inherited controls cannot be modified if the corresponding controls in the base form have the Modifier property set to Private or Friend. But if the Modifier property is set to Public or Protected, the properties of the inherited controls can be modified in the derived form. So if you want to allow derived forms to have the option to change the logo image, set the Modifier property to Public or Protected.
Creating the derived form
Before creating the derived form, be sure to compile the project containing the base form. You should rebuild your project any time changes are made to the base form. To create the derived form, simply add a new inherited form to your project. This will launch the inheritance picker, which will prompt you for the base form. We'll call this new form DerivedForm. It should be created with the logo picture box displayed in the bottom-left corner, just like the base form. As you can see in Figure B, the control sports a little arrow in its corner, which indicates that it is an inherited control.
To see how a change to the base form will affect an inherited form, let's return to the base form and reposition our logo. We'll assume that our fictitious company has now been acquired, and the logo needs to appear in the upper-right corner on all forms.
Move the logo picture box from the bottom-left corner to the upper-right corner. Don’t forget to set the anchoring properties. Then, rebuild the application and ensure that the changes are cascaded into the DerivedForm. That’s all there is to it!