This chapter describes important new features in Visual Basic 5.0, including changes to the interface and changes in the Visual Basic (VB) programming language.
At first glance, the changes in the interface might look daunting. Fortunately, though, the designers of Visual Basic 5.0 have given you an unprecedented number of customizable features. You can make the interface look just like the one for Visual Basic 4.0 and then add new features as you need them until the interface is well nigh unrecognizable. It's up to you.
The IDE is where you'll spend most of your time when you're developing a Visual Basic project. It's where you write code, add components to your project, and design your application's visual interface.
Visual Basic 5.0 adds a number of new features to the IDE to make it even easier to develop applications quickly with a minimum of mental anguish.
The Project Explorer is the where you manage the files that comprise your Visual Basic 5.0 project; it is analogous to the Project window in previous versions of Visual Basic.
The main reason why the Project Explorer is necessary in Visual Basic 5.0 is that you can now have multiple projects open at once. This might seem excessive to a Visual Basic veteran, but it makes sense once you take a look at how control creation works. Because an EXE project and an ActiveX project are two separate things and you need an EXE project to test the ActiveX controls you create, you'll likely use multiple projects quite often.
With all these new files and multiple projects, it can be hard to keep track of what's what. Figure 1.1 displays the hierarchy of files and filename extensions.
Figure 1.1 : File and project hierarchy in Visual Basic 5.0.
Note that both ActiveX project files and EXE project files end in .VBP. This means you'll probably want to add some other identifying characteristic to your project files to differentiate them from each other. You may want to denote project files that contain ActiveX controls by including the word "Control" in their filenames, for example.
For example, say you have a project called StickShift. This project is an ActiveX control for a graphical push button shaped like the stickshift for a 1989 five-speed Jeep Wrangler. For this project, at a minimum, you need a file for the StickShift control itself (StickShift.ctl) and a project file (StickShiftControl.vbp). If StickShift has a custom property sheet, you'd call that file StickShift.pag.
You will almost always use an EXE project in order to test your
ActiveX control project. For the StickShift project, that means
you'll add a project file (TestStickShift.vbp) and a form (TestStickShift.frm).
Finally, your project would require a Project group file, StickShift.vpg,
to house the EXE project and the ActiveX project.
TIP |
It is not a bad idea to put each project you create into its own folder, in order to keep all the files you need in one place. The projects on the CD-ROM that accompanies this book are organized in this way. |
When you launch Visual Basic 5.0 for the first time, the first thing you'll probably notice is that the layout of the windows and menus has changed somewhat. The Visual Basic Integrated Development Environment (IDE) now has a Multiple Document Interface (MDI). The advantage of MDI is that all the subwindows (of which there are many) stay in one big window that stays in the background (called a parent window).
The disadvantage of MDI is that you don't have quite as much flexibility in moving and re-sizing windows. Also, the new interface can be a little jarring when you've been working with Visual Basic for years. Fortunately, there is a way to get the Visual Basic IDE to look like it did in previous versions: with a style known as Single Document Interface (SDI). To change Visual Basic to an SDI interface:
You will have to re-start Visual Basic after doing this, but that's
a small price to pay to return to the decent, red-blooded, God-fearing
Visual Basic interface we've come to know and love for all these
years.
NOTE |
The examples in this book assume that you're using the SDI interface. The instructions for procedures differ only slightly between the two interfaces. You'll be able to figure out what to do if you insist on using the new MDI interface, but for certain things like closing and resizing windows, the SDI interface is easier to use. If you're getting the impression that I have a personal vendetta against the MDI interface, you're right. This is because an MDI interface killed my brother |
The Form Layout window is just below the Properties window on the right side of the screen. It enables you to see just where a form will appear on the screen at runtime by visually positioning forms on the screen. Now instead of having to write the tedious few lines of code to center each form with respect to the screen, you can use the Form Layout window.
By right-clicking on the Form Layout window, you can display resolution guides. These guides show you what your form layout will look like on a standard VGA screen at 640 by 480 resolution. This can be helpful, since many developers have higher-resolution screens, although some users are still trapped in the low-res world. Figure 1.2 shows the location of the Form Layout window on the screen.
Figure 1.2 : The Form Layout window.
By right-clicking on the Form Layout window and selecting Startup Position, you can denote where a form should be located when your application starts. (You need to have a form in the foreground of your project to see this feature in action.)
You can also specify that a form should be centered with respect to the screen at startup time by setting the form's Startup Position property to 2-Center Screen (this is a new property for forms in Visual Basic 5.0).
You probably won't use the Form Layout window much when creating controls using Visual Basic 5.0, unless your control displays forms that act as custom dialog boxes. In that case, you should use the Form Layout window to ensure that your custom dialog box is displayed correctly.
Visual Basic 5.0 gives you the ability to automatically indent or outdent one or more lines of code. This can go a long way toward making your code more readable. And the fact that it's automatic means that you won't have to change the individual indents for 30 lines of code every time you decide to enclose that big code chunk in an If Then statement. To see how automatic indent/outdent works:
You have the ability to move, resize, and customize Visual Basic's toolbars. This feature is not unlike the dockable and customizable toolbars that have existed in Microsoft Office applications for years.
There are four toolbars in Visual Basic: Standard, Edit, Form Editor, and Debug. Visual Basic will generally display whatever toolbar is appropriate for what you're doing. But there are situations where you might want to add a frequently used command to an existing VB toolbar. For example, say you use the Toggle Bookmark command frequently and you'd like the command to appear on the toolbar instead of in the Edit menu. To add a Bookmark button to the Visual Basic toolbar:
Figure 1.3 : Customizing the toolbar.
If you like, at this point you can modify the Toggle Bookmark toolbar button you just created. To do this:
Figure 1.4 : The Modify Selection menu.
Visual Basic 5.0 comes with two new wizards that automate the process of developing controls.
The ActiveX Control Interface Wizard is a tool that automatically inserts property and event code in controls you write. You can use it to add the properties of existing controls to your control (a process known as delegation) or create entirely new properties and methods. Since controls have lots of properties, but the code to implement them is exactly the same, this wizard can take the drudgery out of creating a programmable interface.
The Property Page Wizard is a tool that helps you create property pages for your controls. A property page is a customized version of the Visual Basic property window. Property pages are good ways to give access to a control's properties to its users, particularly if your control has many properties or if it has a few complicated properties.
The ActiveX Control Interface Wizard and the Property Page Wizard are both discussed in more detail in Chapter 4 "Control Properties."
Visual Basic 5.0 gives you the ability to switch between two views of your code. Procedure View displays one procedure at a time, while Full Module View shows all the procedures in a module, one after the other. Procedure View was the default in versions of Visual Basic prior to 5.0; Full Module View is the default in Visual Basic 5.0.
Many programmers who are new to Visual Basic get annoyed because they can't see all their code at once-they're afraid they're going to write code that will later "get lost" in the interface. Full Module View is handy because it lets you compare several adjacent procedures in your code. This is particularly useful when you're defining a new property for a control, since implementing a property always requires at least two procedures (and usually more). To demonstrate the difference between Procedure View and Full Module View:
You'll probably switch between Procedure View and Full Module View frequently as you work with Visual Basic 5.0, depending on what kind of code you're working on. If you're working with a set of small property procedures, you're likely to want to use Full Module View. But if you're slogging through a giant global procedure with lots of lines of code, it might make sense to switch to Procedure View in order to reduce the amount of clutter on your screen.
If you always want code to start in Procedure View (that is, you want Visual Basic 5.0 to work the way previous versions of Visual Basic did), do the following:
In Visual Basic 5.0 you can set bookmarks in code to make it easier to return to a piece of code you're working on or to flip between two pieces of code quickly. You have the ability to move to the next or previous bookmark and to clear all the bookmarks in your project. In this respect, bookmarks work a lot like breakpoints, except they don't cause your code to pause.
To see how bookmarks work:
As you write code in Visual Basic 5.0, you'll notice that helpful
hints will appear on your screen as you type. These hints are
comprised of several features of the Visual Basic 5.0 editor,
collectively known as Automatic Code Completion. Table 1.1 lists
code completion features in Visual Basic 5.0.
Feature | Displays |
Auto List Members | Members of object classes |
Auto Quick Info | Syntax of Visual Basic functions |
Auto Data Tips | Displays the value of the variable under your cursor |
All three elements of automatic code completion are available by default. You can also shut off these features if you wish. To demonstrate how Auto List Members works, open any code window. Type Debug.P. An Auto List Members window will pop up at the point where you were typing, displaying the two members of the Debug object, Assert and Print. (Assert is new to Visual Basic 5.0 and is discussed later in this chapter.) Figure 1.5 shows what an Auto List Members window looks like. Type a Tab to accept Print as the completed statement.
Figure 1.5 : Auto List Members window.
To demonstrate how Auto Quick Info works:
MsgBox
Figure 1.6 : Code completion using Auto Quick Info.
Auto Data Tips are like the ToolTips you might be familiar with if you're a user of any of the Microsoft Office products. In Microsoft Word, for example, when you position your mouse pointer above a toolbar button and wait a second, a ToolTip appears telling you what the button does. The difference is that Visual Basic's Auto Data Tips work only on running code. So to demonstrate how they work, you'll have to create some running code.
Listing 1.1 Code to demonstrate Auto Data Tips
Private Sub Form_Load() Dim ImportantVariable As Integer ImportantVariable = 45 Stop Debug.Print ImportantVariable End Sub
Figure 1.7 : Display of an Auto Data Tip.
I think these features are extremely cool additions to the Visual Basic IDE, but if they bug you, you can turn them off. To turn off these editing features:
In Visual Basic 5.0 you can now use drag-and-drop editing, just as in any Microsoft Office application. Drag-and-drop editing lets you move code quickly and easily. For example, take the code of Listing 1.2.
Say that the Stop statement needs to be moved down a line. You could select it, copy it, reposition the insertion point, and paste it, but that's the old-fashioned, backward-thinking way. Instead, try this:
Listing 1.2 Code to demonstrate drag-and-drop editing
Private Sub Form_Load() Dim ImportantVariable As Integer ImportantVariable = 45 Stop Debug.Print ImportantVariable End Sub
You can use a little-known additional feature of drag and drop, drag-and-copy, to duplicate text. For example, say you need two Stop statements in your code. To do this using drag-and-copy:
If drag-and-drop editing bugs you, you can turn it off. To do this:
The Visual Basic 5.0 programming language contains a number of new keywords, program objects, and concepts. The new language features that apply to control creation are discussed in detail throughout this book and are summarized in a table at the end of this chapter. In addition, here's a quick summary of the features that are not directly pertinent to control creation.
An assertion is a debugging tool designed to conditionally pause your code if a fatal error is raised. Essentially, an assertion tests a condition. The condition can be a variable, a function call, or what have you. If the assertion is True, code continues executing. If the assertion is False, code pauses so you can diagnose the problem.
It's possible to have assertions in virtually any language using a function call, but now Visual Basic supports assertions as a method of the Debug object.
For example, say you have a function called OpenMyFile() that opens a file. The function returns True if it succeeds and False if it fails. You can incorporate the return value of this function in an assertion like this:
Sub AssertOpen() Debug.Assert OpenMyFile("c:\windows\myfile.txt") End Sub
In this code, if the OpenMyFile function failed, the Assert method would pause the code, enabling you to see exactly where the problem took place.
Because your code can only be paused in the development environment, using the Assert method only makes sense in that context. When your code is compiled, any Assert methods remaining in your code are ignored.
An enumerated constant is a concept that programmers from other languages such as C will be familiar with. The idea behind an enumeration is that constants can often be grouped together in categories. Since people have an easier time remembering words than numbers, it makes sense to name, or enumerate, these constants.
An Enum declaration goes in the declarations section of a code module. A typical Enum declaration looks like this:
Public Enum Shoes TennisShoes = 0 HikingBoots = 1 HipWaders = 2 StilletoHeels = 3 End Enum
You can make reference to the values in an enumeration using code that looks like this:
Sub SetShoes() Dim MyShoes As Shoes MyShoes = HipWaders End Sub
You may be thinking that this feature is suspiciously similar to the Type declaration, which has existed in VB forever. Types and Enums are similar, although Enums are used to declare related constants, while Types are used to create a custom variable type. But the place where Enums come in handy is in the area of defining properties for controls.
If you've used Visual Basic in the past, you've probably run across enumerated properties without realizing it. For example, consider the Align property of the Picture Box control. The legal values for an Align property are the numbers 0 through 4. But when you set the Align property control in the Properties window, you're shown these choices:
0 - None 1 - Align Top 2 - Align Bottom 3 - Align Left 4 - Align Right
The values stored by the Align property are the numbers 0 through 4, but the choices displayed by the combo box include enumerated names for each value. You do the same thing for properties in controls you create by using Enums. See Chapter 4 "Control Properties," for information on how to do this.
The AddressOf operator permits the use of callbacks in Windows API and DLL calls. This is a pretty advanced topic that is way beyond the scope of this book, but it's the kind of thing that causes C programmers to either jump up and click their heels or moan in grief. Go figure.
The AddressOf operator was added to Visual Basic 5.0 so VB programmers can place calls to Windows API functions that take function pointers as arguments. There are a few cases in the dark, foreboding world of the Windows API where you'd need to do this; the VB5 Help file demonstrates how you can use the AddressOf operator to generate a list of all the fonts installed on your system.
I was thinking about providing my own code example using AddressOf but realized that I'd approach mandatory retirement age before I was able to come up with a good example of my own; second, it wouldn't be as good as the example in the VB Help file; and third, it wouldn't be pertinent to control creation anyway. So I'll leave it to some other enterprising author to cover Windows API calls using AddressOf.
Table 1.2 summarizes new features of the Visual Basic language
that have specific implications for control creation. All of these
features are discussed in depth elsewhere in this book.
Language Feature | Description | Discussed in |
AmbientProperties Object | Holds properties of a control container to enable a user control to initialize itself | Chapter 4 "Control Properties" |
AsyncRead method,
AsyncProperty object, AsyncReadComplete event | Enables a control to read a property from a source located on the Internet | Chapter 14, with the Internet" "Controls That Interact |
DataBindings collection, DataBinding object, IsBindable property | Enables you to bind your control to databases | Chapter 17, "Database Access" |
Extender object | Holds properties of a control provided by its container | Chapter 4 "Control Properties" |
Hyperlink object, GoBack method, GoForward method, NavigateTo method | Enables you to tell a Web browser object to jump to a particular URL | Chapter 14, "Controls That Interact with the Internet" |
PropertyBag object, ReadProperty method, WriteProperty method. | Stores design-time properties of a user-defined control | Chapter 4 "Control Properties" |
PropertyPage object | Provides a custom property page for your control | Chapter 4 "Control Properties" |
UserControl object | The object used to create your own ActiveX control | Hither and yon throughout the whole book |
Event statement | Defines a custom event | Chapter 5 "Control Events" |
RaiseEvent statement | Fires a user-defined event | Chapter 5 "Control Events" |
Implements statement | Specifies a interface or class that will be a class module | Chapter 16, "Object-Oriented used in Programming" |
This chapter introduced a number of new features of Visual Basic 5.0. In addition to changes in the integrated development environment, a number of new language features were discussed.
Chapter 2is a brief walk-through demonstrating how to set up a very simple control project. In this chapter, you'll learn a number of basic concepts pertaining to control creation while putting the new features of Visual Basic 5.0 into action.