The Form Class
Forms are just classes with windows. Think of it this way: somewhere in the heart of the source code for the Basic language lives a module named FORM.CLS.
You didn’t know that Visual Basic is written in Visual Basic? Well, I have inside information (incorrect, like most inside information) that Visual Basic is actually written in Visual Basic. The FORM.CLS module contains Property Let and Property Get statements for AutoRedraw, BackColor, BorderStyle, Caption, and all the other properties you see in the Properties window. It has public subs for the Circle and Line methods and public functions for the Point method. It defines events with the Event statement. All forms automatically contain a hidden WithEvents statement for their own events and one for each control on the form.
The FORM.CLS module didn’t change much in Visual Basic versions 1 through 3—a few new properties here, a few new methods there. But for version 4, somebody got the bright idea that if a form is just a class, users should be able to add their own properties and methods. By customizing a form with properties and methods, you make it modular. It’s easy to define your own standard forms that can be called from any project.
Get With It
The With statement—stolen from Pascal and modified to fit Basic—is a fancy form of Set. The primary purpose is to make it easier and more efficient to access nested objects. For example, consider this With block:
With Country.State.City.Street.Houses(“My”)
.Siding = ordAluminum
.Paint clrHotPink
.LawnOrnament = “Flamingo”
End With
This is equivalent to the following:
Dim house As CHouse
Set house = Country.State.City.Street.Houses(“My”)
house.Siding = ordAluminum
house.Paint clrHotPink
house.LawnOrnament = “Flamingo”
The With version is more readable, and you don’t have to declare the reference variable. Instead, a hidden one is created for you. Of course, you don’t absolutely need With or Set. You can do the same thing this way:
Country.State.City.Street.Houses(“My”).Siding = ordAluminum
Country.State.City.Street.Houses(“My”).Paint clrHotPink
Country.State.City.Street.Houses(“My”).LawnOrnament = “Flamingo”
This code is not only harder to read but is also much less efficient. Internally, Basic must look up every nested object for every access.
When you’re not using nested objects (and we’re not, in this chapter), the With statement is mostly syntactical sugar. It might save you some line wrapping, particularly if you need to access an object more than once in a line:
With lstHouses
.ItemData(.ListIndex) = Len(.Text)
End With
You might find that more readable than this:
lstHouses.ItemData(lstHouses.ListIndex) = _
Len(lstHouses.Text)
But you won’t see a large difference in performance. In fact, the With statement can actually slow you down in some cases, although usually not enough to change my code style.
The only problem with custom forms is that they can’t be public. You can’t expose your standard forms in components. This turns out to be an advantage. Forms have all sorts of properties and methods that you don’t want clients messing with. If anybody can do anything with your standard form, it’s not standard. The way you expose standard forms in components is to wrap them in a class. The class initializes and controls the form by manipulating the appropriate methods properties, but it blocks clients from doing so except through properties and methods you control. We’ll get to an example of this with a standard About form in Chapter 11.