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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Object Property Setting<br />

frm3_4_7 Caption, ANSI Values<br />

lblPress Caption Press any key<br />

txtCharacter Text (blank)<br />

picOutput<br />

Private Sub txtCharacter_KeyPress(KeyAscii As Integer)<br />

txtCharacter.Text = “”<br />

picOutput.Cls<br />

picOutput.Print Chr(KeyAscii);<br />

“ has ANSI value”; KeyAscii<br />

End Sub<br />

[Run, <strong>and</strong> then press a key. For instance, if A is pressed, the following is displayed in the picture box.]<br />

A has ANSI value 65<br />

The KeyPress event procedure can alter the character typed into the text box. For<br />

instance, if the statement<br />

KeyAscii = 65<br />

is placed in a KeyPress event procedure, the letter A is displayed when any st<strong>and</strong>ard key is<br />

pressed. In Section 4, we use a decision structure to prevent the user from typing unwanted keys.<br />

For instance, if we want the user to enter a number into a text box, we can intercept <strong>and</strong> discard<br />

any key presses that are not digits. The statement<br />

KeyAscii = 0<br />

placed in a KeyPress event procedure discards the key pressed. Finally, a program can be made<br />

more friendly by letting the Enter key (ANSI value 13) move the focus in the same way that<br />

the Tab key moves the focus. This requires having a KeyPress event procedure for each object<br />

that is to respond to the Enter key <strong>and</strong> then setting the focus to the next object when the value<br />

of KeyAscii is 13.<br />

COMMENTS<br />

1. The string “” contains no characters <strong>and</strong> is called the null string or the empty<br />

string. It is different than the string containing a single space (“ ”). String variables<br />

that have not been assigned values initially have “” as their default values.<br />

(Numeric variables have default value 0.)<br />

2. The statement picBox.Print, with no string or number, simply skips a line in the<br />

picture box.<br />

3. Assigning a string value to a numeric variable can result in the error message<br />

“Type mismatch.”<br />

4. In <strong>Visual</strong> <strong>Basic</strong> 6.0, the maximum allowable number of characters in a string is<br />

approximately 2 billion.<br />

5. The quotation-mark character (“) can be placed into a string constant by using<br />

Chr(34). For example, after the statement<br />

txtBox.Text = “George ” & Chr(34) & “Babe” & Chr(34) & “ Ruth”<br />

is executed, the text box contains<br />

George “Babe” Ruth<br />

Strings 47

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

Saved successfully!

Ooh no, something went wrong!