The magazine of the Melbourne PC User Group

Seeing the Basics: Did you know?
Tony Stevenson

This month's column continues with a look at some of the different and interesting aspects of the Visual Basic language, all presented together under the general title of "Did You Know?".

A quick way of changing properties

One of Visual Basic's strengths is undoubtedly its use of properties to alter the behaviour and appearance of Windows' objects. However, it can be slightly annoying, not to mention time consuming, when you want to change the same property on a largish number of controls on a form.

For example, you might have a dozen TextBoxes on a form which are to be invisible when your program first executes. In design mode, rather than setting the Visible property of each TextBox individually, do the following instead. Left-click the first TextBox using your mouse, and then, while holding down the Ctrl key on the keyboard, left-click all of the remaining TextBoxes. As you click each one, you'll notice that its border will change colour.

Once you've clicked all the TextBoxes, press the F4 key. This will cause the Property window to open and display. Now to set the Visible property, simply double-click its caption in the Property window to toggle between the possible values of True and False.

Try this procedure again, but this time click on a TextBox and a command button on a form instead. Even though these two types of control have their own unique properties, this Ctrl key method still allows you to change those properties quickly, like the Visible property, which are common to both controls.

Bookmark your VB code

As your Visual Basic programs become more complex, they inevitably grow larger in size, too. This increase in the number of lines of code can sometimes make it frustrating to find particular sections of code quickly.

However, Visual Basic 5.0 easily solves the problem with its use of "bookmarks", just one of the new functions of the Code Editor. As its name suggests, this feature allows a nominated line of code to be associated with a bookmark for quick reference. Multiple bookmarks can be set up for any project.

To see how bookmarks work, open the code window of any Visual Basic program, and select a line of code by left clicking it with the mouse.

Next, click "Edit" on the main VB design menu, and scroll down until the mouse cursor is over the last entry, "Bookmarks". From the subsequent cascading menu, click "Toggle Bookmark". The selected line of code is now identified as a bookmark by a coloured indicator displayed in the left hand margin of the code window.

It's easy to remove a bookmark. Simply select the line of code which has been designated as a bookmark, and follow the procedure outlined above to toggle it off.

However, a quicker, more convenient way of toggling bookmarks on and off is to right click a selected line of code using your mouse, and then to click the "Toggle" and "Bookmark" options from the subsequent menus (see Figure 1). Note also how the "Toggle" option can be used to set breakpoints on and off as well.

Once a number of bookmarks have been inserted into the code, it's then possible to move backwards and forwards between them using the "Next Bookmark" and "Previous Bookmark" on the "Bookmarks" option of the "Edit menu". From there, it's also possible to clear all bookmarks with a single click of your mouse using "Clear All Bookmarks".


Figure 1. Using bookmarks to navigate around VB code.


Figure 2. Advanced alignment capabilities available on VB's format

Designing forms quickly

Another of Visual Basic's great strengths is that creating a program's graphical user interface is largely just a matter of pointing and clicking with your mouse.

But by using the new functions available on Visual Basic 5.0's Format menu (see Figure 2), interface design has now been made even simpler through the use of its advanced alignment capabilities.

To see how these functions work, start a new standard VB 5.0 project and add three command buttons, of different sizes, to its default form.

Firstly, let's experiment by making the command buttons all the same size.

To do so, first select all the command buttons (see discussion above about how to simultaneously select multiple controls).

Next, click the "Format" option on VB's main design menu, and then select the option "Make Same Size". The cascading menu that now appears provides the options of making the widths of all the command buttons the same, or their heights the same, or both their widths and heights the same. Click the last option, and voila!--all three command buttons are now identical.

This time, let's align the command buttons. As before, make sure all the buttons are selected, and again select the "Format" option. Then click "Align", followed by the "Lefts" from the subsequent cascading menu.

The result is that, with very little effort, the command buttons are now neatly aligned. Options available here also enable controls to be moved to the centre, right, top, middle, bottom, and so on.

Experiment yourself with the other different sorts of alignment options available on the "Format" menu. For example, try altering the horizontal or vertical spacing between selected controls on a form.

By providing these advanced alignment capabilities, VB 5.0 frees programmers from the more tedious aspects of interface design, and instead allows them to concentrate on writing the associated code.

Visual Basic 6.0 has been released

Next month's "Seeing the Basics" will take an in-depth look at the latest release of Visual Basic.

Reprinted from the October 1998 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]