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.

14. Click on the text box or press the Tab key to move the cursor (<strong>and</strong>, therefore,<br />

the focus) to the text box. This calls the event procedure txtPhrase_GotFocus,<br />

which restores the text to its original state.<br />

15. You can repeat Steps 11 through 14 as many times as you like. When you are<br />

finished, end the program by pressing Alt+F4, clicking the End icon on the<br />

Toolbar, or clicking the Close button (X) on the form.<br />

COMMENTS<br />

1. To hide the code window, press the right mouse button <strong>and</strong> click on Hide. You<br />

can also hide it by clicking on the icon at the left side of the title bar <strong>and</strong> clicking<br />

on Close. To view a hidden code window, press Alt/View/Code. To hide a<br />

form, close its container. To view a hidden form, press Alt/View/Object.<br />

2. The form is the default object in <strong>Visual</strong> <strong>Basic</strong> code. That is, code such as<br />

Form1.property = setting<br />

can be written as<br />

property = setting<br />

Also, event procedures associated with Form1 appear as<br />

Form_event()<br />

rather than<br />

Form1_event()<br />

3. Another useful comm<strong>and</strong> is SetFocus. The statement<br />

object.SetFocus<br />

moves the focus to the object.<br />

4. We have ended our programs by clicking the End icon or pressing Alt+F4. A<br />

more elegant technique is to create a comm<strong>and</strong> button, call it cmdQuit, with<br />

caption Quit <strong>and</strong> the event procedure:<br />

Private Sub cmdQuit_Click()<br />

End<br />

End Sub<br />

5. Certain words, such as Sub, End, <strong>and</strong> False, have special meanings in <strong>Visual</strong><br />

<strong>Basic</strong> <strong>and</strong> are referred to as keywords or reserved words. The <strong>Visual</strong> <strong>Basic</strong> editor<br />

automatically capitalizes the first letter of a keyword <strong>and</strong> displays the word<br />

in blue.<br />

6. <strong>Visual</strong> <strong>Basic</strong> can detect certain types of errors. For instance, consider the line<br />

txtPhrase.Font.Bold = False<br />

from the walkthrough. Suppose you neglected to type the word False to the right<br />

of the equal sign before leaving the line. <strong>Visual</strong> <strong>Basic</strong> would tell you something<br />

was missing by displaying the left message box at the top of page 66. (Also, the<br />

line would turn red.) On the other h<strong>and</strong>, suppose in the CmdBold_Click procedure<br />

you misspell the keyword “Bold” as “bolt.” You might notice something is<br />

wrong when the letter “b” is not capitalized. If not, you will certainly know<br />

about the problem when the program is run because <strong>Visual</strong> <strong>Basic</strong> will display<br />

the right message box at the top of page 66 when you click on the comm<strong>and</strong><br />

<strong>Visual</strong> <strong>Basic</strong> Events 33

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

Saved successfully!

Ooh no, something went wrong!