The magazine of the Melbourne PC User Group

Seeing the Basics: Finding procedure
definitions easily

Tony Stevenson

Some of Visual Basic's critics argue that it is easy for code to get lost in a VB project, especially as projects become larger and more complex. However, VB 4.0 provides an elegant solution for easily finding procedure definitions.

Consider the following simple program, which includes a function to add two numbers, as well as a subroutine to display a message saying that the program is finished. For this example these two routines are set up in a separate code module (.BAS file) from the one form used in the project. The form itself contains just a single command button used to initiate the addition process. When the program is executed and the command button is clicked, the result of the addition is printed in the top left-hand corner of the form.

Private Sub Command1_Click ()
Dim x as Integer, y as Integer, z as Integer
x = 10
y = 20
z = addTwoNumbers(x, y)
Print z
displayFinished
End
End Sub


The following two routines are stored in Module1 of the project.

Function addTwoNumbers (x As Integer, y As Integer)
addTwoNumbers = x + y
End Function

Sub displayFinished ()
MsgBox "We are now finished"
End Sub

Imagine now that these routines are part of a much larger project which has been passed on to you to look after. A modification to the program has been requested which involves changing both the addTwoNumbers and displayFinished routines.

One way of locating a function or procedure (in order to edit it) is to use the Find command contained on the Edit menu.

For example, to find the function definition for addTwoNumbers, a lot of VB programmers will double-click on the command button to bring up the code window for the Command1 click event. Next they will highlight the character string addTwoNumbers, click the Edit menu command, then the Find menu option, click the character string addTwoNumbers in the Find What combo box of the Find dialog box (so as to de-select the string), type in "Function" before the function name to be searched for, and finally click the Find Next command button. (Make sure the Search Current Project radio button is also set.)

Whew! This way works but obviously it is a cumbersome approach to locating a function definition.

A better, and preferred way is to use the Procedure Definition command option located under the View main menu option. To see how this works, once again display the contents of the code window for the click event of the command button in our sample project. Highlight the character string addTwoNumbers as before, but this time click the View menu command followed by the Procedure definition option.

The result of the search is that the cursor is now positioned at the start of the addTwoNumbers function, ready for editing

This same search mechanism works not only for function definitions but also for subroutine definitions as well. Why not try it now with the displayFinished call in the click event of the command button? However, this time use the keyboard shortcut combination of Shift and F2 (after highlighting the character string displayFinished) to make finding this sub procedure definition even easier.

Finally, there is one more technique (tip) for making the process even faster. Instead of highlighting the character string of the function or subroutine definition to be searched for, just position the cursor anywhere in the string before pressing the Shift and F2 keys.

Bumping your controls

In design mode, the usual practice for resizing controls or moving them is to do so with the mouse. When a control is clicked, its sizing handles automatically appear (these are the small black squares which appear on the border of the selected control).

However, instead of resizing a control by clicking and dragging on one of these black squares, it is possible to "bump" the control to its desired size. This is done by holding down the Shift key on the keyboard and hitting one of the four arrow keys in the required direction. The change in the size of the control is one grid increment / decrement (depending on the direction selected) per each hit of the arrow key.

Likewise, it is possible to bump a control to a new position. Again click the control to be moved to display its sizing handles. To move it, hold down the keyboard Ctrl key instead of the Shift key, and again use the arrow keys to move the control in the desired direction.

Bumping is a convenient and useful way of making small changes in the size of a control, or alternatively, for moving a control a short distance on the interface.

The grid which is displayed on a form can be toggled on and off via VB's design menu structure. Click the Tools menu command, followed by the Options command, and then click the Show Grid check box on the Environment tab.

Get With It!

One of the new statements in VB 4.0 is the With statement. Its generic form is

With Object
   Vb statements go here
End With


To see the advantages of using this statement set up a sample project containing one form containing two command buttons and a label. It doesn't matter where these controls are placed on the form. Set the caption for the first and second buttons to "Old Way", and "New Way" respectively.

Now set up the code for the project as follows:

Private Sub Command1_Click ()
Label1.Caption = "Old Way Of Setting Properties"
Label1.BorderStyle = 1
Label1.Left = 3840
Label1.Top = 0
Label1.Height = 1000
End Sub
Private Sub Command2_Click ()
With Label1
.Caption = "New Way Of Setting Properties"
.BorderStyle = 1
.Left = 3840
.Top = 0
.Height = 1000
End With
End Sub


Even though the code in both click events achieves exactly the same result, the new way of coding has definite advantages. Firstly, it increases the readability of the program because of its logical grouping, making it both simpler to follow and easier to change. Secondly, and especially for larger projects, the time to enter code goes down because there is less code to enter.

Be careful though to include the full stops - if any of these are left out, the program will either not work as expected, or will abort issuing the following error message:
"Function call on left-hand side of assignment must return Variant or Object"

Reprinted from the March 1997 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]