Visual Basic Online Help Example Errors

ID Number: Q78772

1.00

WINDOWS

docerr

Summary:

There are several code examples in Visual Basic's online Help that do

not behave as expected if actually copied and run.

Note: The corresponding examples in the "Visual Basic: Language

Reference" manual contain the same errors.

This information applies to the Microsoft Visual Basic programming

system version 1.0 for Windows.

More Information:

Under the topic "ActiveControl, Active Form Properties," the second

example demonstrating these properties contains an omission of the

Clipboard object. When copied and run as is, the error message

"Method Not Applicable For This Object" will be displayed.

The ActiveControl example contains the line in the EditCut_Click event

procedure for the menu item:

SetText Screen.ActiveControl.SelText

This should be changed to read:

Clipboard.SetText Screen.ActiveControl.SelText

Under the topic titled "Fonts Property," the example shows setting the

FontName = "". This will cause a run time error "Invalid Property

Value".

The example will also fail when attempting to select a printer

FontName as the screen FontName where no associated screen font exists

under Windows. For example, when the printer LinePrinter font is

selected for the screen, an error will occur because the screen does

not support this font. The examples for the topics "FontName Property"

and "FontCount Property" if modified as suggested in the online Help

to print the available printer fonts to the screen will fail for the

same reason.

The example for the Fonts Property follows:

'Fonts Property Example

Sub Form_Click ()

Static X% ' A static variable.

AutoRedraw = -1 ' Keep screen text.

If X% = Printer.FontCount Then ' Check for last font.

X% = 0 ' Set X%.

Print ' Print blank line.

FontName = "" ' Reset to default font.

End If

If X% = 0 Then Print "Printer Fonts" ' Print header.

FontName = Printer.Fonts(X%) ' Set FontName.

Print X%; "This is " + FontName + " font" ' Print message.

X% = X% + 1 ' Set X%.

End Sub

As stated above, this fails in two ways. The line resetting the

FontName property is syntactically incorrect. Also, the logic may

fail because of no corresponding screen font for the printer font.

Modifying the example to address both problems requires an On Error

trap routine and saving the values of FontName, FontBold, and FontSize

explicitly. The following example works properly.

'Fonts Property Example

Sub Form_Click ()

On Error GoTo errHandler

Static x% ' A static variable.

Static savename$, savebold%, savesize' <<< added this!!

AutoRedraw = -1 ' Keep screen text.

If x% = Printer.fontcount Then ' Check for last font.

x% = 0 ' Set X%.

FontName = savename$ ' <<<add this! Reset to default font.

Fontsize = savesize ' and this

Fontbold = savebold% ' and this

Print ' Print blank line.

End If

If x% = 0 Then

Print "Printer Fonts" ' Print header.

savename$ = FontName ' save all these

savebold% = Fontbold ' to original settings

savesize = Fontsize ' now

End If

FontName = Printer.Fonts(x%) ' Set FontName.

Print x%; "This is " + FontName + " font" ' Print message.

ExitSub:

x% = x% + 1 ' Set X%.

Exit Sub

errHandler:

Print x%; "This is " + Printer.Fonts(x%) + " name"

Resume ExitSub:

End Sub

Additional reference words: 1.00