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.

464 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

6. Click on the End icon to end the program.<br />

7. If desired, step through the program again, entering a different age <strong>and</strong> predicting<br />

which Case clause will be acted upon. (Some possible ages to try are 12, 14,<br />

18, 33, <strong>and</strong> 67.)<br />

STEPPING THROUGH A PROGRAM CONTAINING A DO LOOP: SECTION 5<br />

Do Loops<br />

The following walkthrough demonstrates use of the Immediate window to monitor the value<br />

of a condition in a Do loop that searches for a name.<br />

1. Access Windows’ Notepad, enter the following line of data, <strong>and</strong> save the file on<br />

the A drive with the name DATA.TXT<br />

Bert, Ernie, Grover, Oscar<br />

2. Return to <strong>Visual</strong> <strong>Basic</strong>. Create a form with a comm<strong>and</strong> button (cmdButton) <strong>and</strong><br />

a picture box (picBox). Set the AutoRedraw property of the picture box to True.<br />

Then double-click on the comm<strong>and</strong> button <strong>and</strong> enter the following procedure:<br />

Private Sub cmdButton_Click()<br />

‘Look for a specific name<br />

Dim searchName As String, nom As String<br />

picBox.Cls<br />

searchName = InputBox(“Name:”) ‘Name to search for in list<br />

Open “A:DATA.TXT”For Input As #1<br />

nom = “”<br />

Do While (nom [%6][lt>]20searchName) And Not EOF(1)<br />

Input #1, nom<br />

Loop<br />

Close #1<br />

If nom = searchName Then<br />

picBox.Print nom<br />

Else<br />

picBox.Print “Name not found”<br />

End If<br />

End Sub<br />

3. Press F8 <strong>and</strong> click on the comm<strong>and</strong> button. The heading of the event procedure<br />

is highlighted in yellow.<br />

4. Double-click on the variable searchName, click the right mouse button, click on<br />

“Add Watch,” <strong>and</strong> click on OK. The variable searchName has been added to the<br />

Watch window.<br />

5. Repeat Step 4 for the variable nom.<br />

6. Drag the mouse across the words<br />

(nom searchName) And Not EOF(1)<br />

to highlight them. Then click the right mouse button, click on “Add Watch,” <strong>and</strong><br />

click on OK. Widen the Watch window as much as possible in order to see the<br />

entire expression.<br />

7. Press F8 three more times to execute the picBox.Cls statement <strong>and</strong> the statement<br />

containing InputBox. Enter the name “Ernie” at the prompt.<br />

8. Press F8 repeatedly until the entire event procedure has been executed. Pause<br />

after each keypress <strong>and</strong> notice how the values of the expressions in the Watch<br />

window change.<br />

9. Click on the End icon to end the program.

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

Saved successfully!

Ooh no, something went wrong!