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.

■ USING TEXT BOXES FOR INPUT AND OUTPUT<br />

The contents of a text box is always a string. Therefore, statements such as<br />

<strong>and</strong><br />

strVar = txtBox.Text<br />

txtBox.Text = strVar<br />

can be used to assign the contents of the text box to the string variable strVar <strong>and</strong> vice versa.<br />

Numbers are stored in text boxes as strings. Therefore, they should be converted to numbers<br />

before being assigned to numeric variables. If str is a string representation of a number,<br />

then<br />

Val(str)<br />

is that number. Conversely, if num is a number, then<br />

Str(num)<br />

is a string representation of the number. Therefore, statements such as<br />

<strong>and</strong><br />

numVar = Val(txtBox.Text)<br />

txtBox.Text = Str(numVar)<br />

can be used to assign the contents of the text box to the numeric variable numVar <strong>and</strong> vice versa.<br />

Note: When a non-negative number is converted to a string with Str, its first character (but not<br />

its last character) is a blank space.<br />

EXAMPLE 6<br />

The following program converts miles to furlongs <strong>and</strong> vice versa. Note: A furlong is 1/8th of a mile.<br />

Object Property Setting<br />

frm3_4_6 Caption, Convertor<br />

lblMile Caption Miles<br />

txtMile Text 0<br />

lblFurlong Caption Furlongs<br />

txtFurlong Text 0<br />

The two text boxes have been named txtMile <strong>and</strong> txtFurlong. With the Event procedures shown, typing a<br />

number into a text box <strong>and</strong> pressing Tab results in the converted number being displayed in the other text<br />

box.<br />

Private Sub txtMile_LostFocus()<br />

txtFurlong.Text = Str(8 * Val(txtMile.Text))<br />

End Sub<br />

Private Sub txtFurlong_LostFocus()<br />

txtMile.Text = Str(Val(txtFurlong.Text) / 8)<br />

End Sub<br />

■ ANSI CHARACTER SET<br />

Each of the 47 different keys in the center typewriter portion of the keyboard can produce two<br />

characters, for a total of 94 characters. Adding 1 for the space character produced by the space<br />

bar makes 95 characters. These characters have numbers ranging from 32 to 126 associated with<br />

Strings 45

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

Saved successfully!

Ooh no, something went wrong!