19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

EXAMPLE 4<br />

In the following solution to Example 1, the file name is provided by the user in an input box.<br />

Private Sub cmdDisplay_Click()<br />

Dim fileName As String, prompt As String, title As String<br />

Dim houseNumber As Single, street As String<br />

picAddress.Cls<br />

prompt = “Enter the name of the file containing the information.”<br />

title = “Name of File”<br />

fileName = InputBox(prompt, title)<br />

Open fileName For Input As #1<br />

Input #1, houseNumber<br />

Input #1, street<br />

picAddress.Print “The White House is located at”; houseNumber; street<br />

Close #1<br />

End Sub<br />

[Run, <strong>and</strong> then click the comm<strong>and</strong> button. The input box of Figure 2-25 appears on the screen. Type<br />

DATA.TXT (possibly preceded with a path) into the input box <strong>and</strong> click on OK. The input box disappears<br />

<strong>and</strong> the following appears in the picture box.]<br />

The White House is located at 1600 Pennsylvania Ave.<br />

The response typed into an input box is treated as a single string value, no matter what<br />

is typed. (Quotation marks are not needed <strong>and</strong>, if included, are considered as part of the<br />

string.) Numeric data typed into an input box should be converted to a number with Val<br />

before it is assigned to a numeric variable or used in a calculation.<br />

■ FORMATTING OUTPUT WITH PRINT ZONES<br />

Each line in a picture box can be thought of as being subdivided into zones, as shown in Figure<br />

2-27. Each zone contains 14 positions, where the width of a position is the average width of<br />

the characters in the font.<br />

FIGURE 2-27 Print Zones<br />

We have seen that when the Print method is followed by several items separated by semicolons,<br />

the items are displayed one after another. When commas are used instead of semicolons,<br />

the items are displayed in consecutive zones. For instance, if the Font property of<br />

picBox is set to Courier, when the motto of the state of Alaska is displayed with the statements<br />

picBox.Print “North”, “to”, “the”, “future.”<br />

picBox.Print “12345678901234567890123456789012345678901234567890”<br />

the resulting picture box is<br />

where each word is in a separate print zone. This same output can be achieved with the code<br />

Input <strong>and</strong> Output 53

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!