The magazine of the Melbourne PC User Group

Seeing the Basics - VB4 tips
Tony Stevenson

This column provides techniques, tips and tricks to help you gain familiarity and experience with Microsoft's Visual Basic - a popular language that allows you to write Windows applications quickly.

Executing another application from within a VB application

Sometimes you want to execute another Windows application from within your VB application. This is achieved by using the "WinExec" API function.

As an example, consider a VB 4 32-bit application consisting of a single form that contains just one command button. Clicking this command button causes the Windows 95 applet "WordPad" to launch.

Use the "Apilod32.exe" (found in the "winapi" folder that comes supplied with VB 4) to locate the "WinExec" function. Copy it into the general declaration section of the form. Be sure to declare it as a "Private" function otherwise you will get the following error message:

"Constants, fixed-length strings, arrays, and Declare statements not allowed as Public members of class or form modules".

The code should look like this:

Private Declare Function WinExec Lib "kernel32"
(ByVal lpCmdLine as String, ByVal nCmdShow As Long) As Long


(Note that it should appear as one continuous physical line in your code.)

The first parameter, "lpCmdLine" is used to hold the details of the command to be executed - which in this case is the path name to, and the executable file name of WordPad. The second parameter dictates how the application to be initiated is to be displayed. Here the parameter "nCmdShow" is given the value of three, which tells Windows 95 to display the launched application as a maximised, activated Window.

Add the constant declaration,
"Const SW_SHOWMAXIMIZED = 3" after the function declaration for "WinExec".

Then the following code should be added to the "Click" event of the command button.

Private Sub Command_Click ()
Dim cmdDetails as String
Dim results as Long
cmdDetails = "C:\progra~1\access~1\wordpad.exe
result = WinExec (cmdDetails, SW_SHOWMAXIMIXED)
EndSub

Note the format of the long folder names (progra~1 and access~1) that are now permissible with Windows 95.

Note also that the variable "result" needs to be declared as "Long", and that the brackets around the parameters for the "WinExec" function call are required.

To guard against errors, for example, a missing WordPad application or WordPad being in another folder, the following error checking should be incorporated into the program. If an error does arise out of the execution of the "WinExec" API function, the value returned in the variable "result" will be less than 32.

result = EinExec (cmdDetails, SW_SHOWMAXIMIZED)
If result 32 Then
  MsgBox "Problem executing the WinExec API"
  '  Appropriate action should be taken here to
  '  recover from this error situation
End if

It is possible to extend this program so that any word processing activity that is carried out using WordPad is made available to this VB application.

The simplest approach would be for the user to place the completed text from WordPad onto the clipboard using the conventional Windows "Copy" command.

Then, from within the VB application, the information on the clipboard could be retrieved using the "GetText" method.

To achieve this, add a text box and another command button to the form. Add the following code to the "Click" event of this button:

Private Sub Command2_Click ()
Text1.Text = Clipboard.GetText
End Sub

Note that if you put the line of code "Text1.Text = Clipboard.GetText" into the "Click" event of the first command button (somewhere after the "WinExec" function call), then you will get erroneous results. The reason for this is that the VB application continues to execute any remaining lines of code in the "Click" procedure after calling the "WinExec" function.

Adding a splash screen to your application

A "splash screen" is the first window seen when a user launches an application. One well-known example is the Microsoft logo screen displayed whilst Windows is loading.

Because of VB's powerful graphical capabilities, the splash screen provides VB programmers with an excellent opportunity to display both their professionalism and creativity.

From a functional perspective, an attractive, well-designed splash screen can also be used to pleasantly distract users whilst any lengthy initialisation procedures are being completed.

Altering tabbing at run time

When a VB application is running, you can use the Tab button on the keyboard to "tab to" (position to) a control that you want to receive the input focus. This facility is useful in situations where a user feels more comfortable using the keyboard than the mouse. For example, in data entry applications. The order of the tabbing sequence is controlled by a number contained in the "TabIndex" property of the control.

Consider a program where four command buttons have been added to a form. Their TabIndex properties would contain the values 0, 1, 2, and 3 respectively.

When this simple program runs, the input focus would be on the first command button (because it has the lowest value of 0 for the TabIndex). Pressing the tab key would move the input focus to the second command button, then pressing it again to the third, and so on in a cyclic nature. Pressing the "Enter" key for any command button with the input focus would then cause any code in its "Click" event to be processed.

At run time, there will be occasions when you want to alter the tabbing sequence. This is easily achieved by setting the "TabStop" property of the appropriate control to False. Any control with this property set to False will be skipped over.

Adding controls quickly at design time

When designing your interface, here is a quick and easy way to add multiple controls, of the same type, to a form.

Display the toolbox. Whilst holding down the "Ctrl" key, select a control, e.g. a command button, using the mouse. Release the "Ctrl" key. Place the first of the controls on the form by holding down the left mouse button and drawing the control to its required shape. Now move the mouse pointer to another location on the form and repeat this drawing process until all of the controls have been positioned.

An alternative approach favoured by some VB programmers is to double click the desired control in the tool box. This will place the control in the centre of the form. By repeating this double clicking procedure, a stack of these controls will then begin to pile up. To position the controls on the form, click on the pile and move the controls, one at a time, to the desired locations. One advantage of this technique is that the pile of controls can consist of all different types.

Reprinted from the June 1996 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]