Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Copyright 2011 Pearson Addison-Wesley

Addison Wesley
is an imprint of
2011 Pearson Addison-Wesley. All rights reserved.
Addison Wesley
is an imprint of
Chapter 3
Variables and Calculations
Copyright 2011 Pearson Addison-Wesley
Addison Wesley
is an imprint of
2011 Pearson Addison-Wesley. All rights reserved.
Addison Wesley
is an imprint of

Section 3.1
Gathering Text Input

In this section, we use the TextBox control


to gather input the user

has typed on the eyboard. !e also alter a


"or#$s tab order and assign

eyboard access eys to controls.


Copyright 2011 Pearson Addison-Wesley
The TextBox Control

% text box is a rectangular area on a "or# that


accepts input "ro# a eyboard

Tutorial 3&1 pro'ides an exa#ple in the use o"


a text box
Chapter 3& Slide 3
Copyright 2011 Pearson Addison-Wesley
(sing the Text )roperty in Code

The TextBox control$s Text property can be


accessed in code the sa#e way you access
other properties

*or +xa#ple,

The contents o" the Text property can be assigned


into a -abel control$s Text property,

lblInfo.Text = txtInput.Text

The contents o" the Text property can be displayed


in a #essage box

MessageBox.Show(txtInput.Text)
Chapter 3& Slide .
Copyright 2011 Pearson Addison-Wesley
Clearing a Text Box

Can be done with an assign#ent state#ent,

txtInput.Text = String.Empty

assigning the prede"ined constant String.+#pty


replaces whate'er text was in txtInput with an
e#pty string

Can also be done with a #ethod,

txtInput.Clear()

Clear is a Method, not a Property

/ethods are actions 0 as in clearing the text

(ses the "or# Object.Method


Chapter 3& Slide 1
Copyright 2011 Pearson Addison-Wesley
String Concatenation

%ssu#e the user has entered their na#e into


the TextBox txt2a#e

-abel lblGreeting can say, 34ello5 to any na#e


"ound in the TextBox

lblreeting.Text = !"ello ! # txt$ame.Text

%ppends user na#e in txt2a#e.Text to 34ello 5


and stores result in text property o" lblGreeting
Chapter 3& Slide 6
Copyright 2011 Pearson Addison-Wesley
String Concatenation

Tutorial 3&7 pro'ides another exa#ple o" how


to concatenate strings "ro# text boxes
Chapter 3& Slide 8
txtDayOfWee
k
txtMonth
txtDayOfMont
h
txtYear
lblDateString
btnExit
btnClear
btnShowDate
Copyright 2011 Pearson Addison-Wesley
%ligning Controls in 9esign /ode

!hen dragging a control to a "or#, it can be


aligned with a control already on the "or#

Blue guide lines appear "or 'ertical align#ent

-a'ender guide lines "or hori:ontal align#ent


Chapter 3& Slide ;
Copyright 2011 Pearson Addison-Wesley
The *ocus /ethod

*or a control to ha'e the "ocus #eans that it is


ready to recei'e the user<s input

In a running "or#, one and only one o" the


controls on the "or# #ay ha'e the "ocus

=nly a control capable o" recei'ing so#e sort


o" input #ay ha'e the "ocus

The "ocus can be set to a control in code using


the *ocus #ethod,
txt%ser$ame.&o'us()
Chapter 3& Slide >
Copyright 2011 Pearson Addison-Wesley
The *ocus /ethod

?ou can tell which control has "ocus by its


characteristics,

!hen a TextBox has "ocus, it will ha'e a blining


cursor or its text will be highlighted

!hen a button, radio button, or a chec box has


"ocus, you$ll see a thin dotted line around the
control

Tutorial 3&3 shows an exa#ple o" the *ocus


#ethod
Chapter 3& Slide
1@
Copyright 2011 Pearson Addison-Wesley
Controlling a *or#$s Tab =rder
with the TabIndex )roperty

Tab ey steps "ocus "ro# one control to the


next

This order is set by the TabIndex property

The Tab ey causes the "ocus to Au#p to the


control with the next highest TabIndex 'alue

The TabIndex property is best changed with


the Tab =rder option "ro# the View #enu

9isplays the "or# in tab order selection #ode

Set a new tab order by clicing the controls in the


order you want

This sets the nu#eric TabIndex 'alue


Chapter 3& Slide
11
Copyright 2011 Pearson Addison-Wesley
%ssigning Beyboard %ccess Beys to Buttons

Say your "or# had a button with the text


3+xitC on it

?ou can allow the user to acti'ate the button


using %lt&D instead o" a #ouse clic

Eust change the button text property to


3+FxitC

The character "ollowing the <F< Gx in this caseH


is designated as an access ey

Be care"ul not to use the sa#e access ey "or


two di""erent buttons
Chapter 3& Slide
17
Copyright 2011 Pearson Addison-Wesley
<F< 4as Special /eaning in a
Button

2ote that the <F< in


3+FxitC does not display
in the button control on
the "or#

It si#ply establishes the


%lt Bey access

In order to actually
display an <F< on a button,
it #ust be entered as
CFF3

Button text Save & Exit is


entered as Save && Exit
Chapter 3& Slide
13
Copyright 2011 Pearson Addison-Wesley
Setting the %ccept Button

The accept button is a button that is i#plicitly


acti'ated i" the user hits the +nter Bey

The %cceptButton )roperty designates which


button on the "or# will beha'e in this #anner

The button cliced #ost "reIuently on a "or#


is usually assigned as the accept button
Chapter 3& Slide
1.
Copyright 2011 Pearson Addison-Wesley
Setting the Cancel Button

The cancel button is a button that is i#plicitly


acti'ated i" the user hits the +scape Bey

The CancelButton )roperty designates which


button on the "or# will beha'e in this #anner

%ny exit or cancel button on a "or# is a


candidate to beco#e the cancel button

Tutorial 3&1 pro'ides exa#ples o" setting


access eys, accept, and cancel buttons
Chapter 3& Slide
11
Copyright 2011 Pearson Addison-Wesley
Addison Wesley
is an imprint of
2011 Pearson Addison-Wesley. All rights reserved.
Addison Wesley
is an imprint of

Section 3.7
Variables and 9ata Types

Variables hold data that #ay be


#anipulated, used to #anipulate

other data, or re#e#bered "or later use.


Copyright 2011 Pearson Addison-Wesley
!hy 4a'e VariablesJ

% 'ariable is a storage location in the


co#puter$s #e#ory, used "or holding
in"or#ation while the progra# is running

The in"or#ation that is stored in a 'ariable


#ay change, hence the na#e 3'ariable5
Chapter 3& Slide
18
Copyright 2011 Pearson Addison-Wesley
!hat Can ?ou 9o !ith VariablesJ

Copy and store 'alues entered by the user, so


they #ay be #anipulated

)er"or# arith#etic on 'alues

Test 'alues to deter#ine that they #eet so#e


criterion

Te#porarily hold and #anipulate the 'alue o"


a control property

Ke#e#ber in"or#ation "or later use in the


progra#
Chapter 3& Slide
1;
Copyright 2011 Pearson Addison-Wesley
4ow to Thin %bout Variables

?ou the progra##er #ae up a na#e "or the


'ariable

Visual Basic associates that na#e with a


location in the co#puter<s K%/

The 'alue currently associated with the


'ariable is stored in that #e#ory location
Chapter 3& Slide
1>
Copyright 2011 Pearson Addison-Wesley
9eclaring Variables

% 'ariable declaration is a state#ent that creates a 'ariable in


#e#ory

The syntax is,


(im )ariable$ame *s (ataType

(im Gshort "or 9i#ensionH is a eyword

)ariable$ame is the progra##er designated na#e

*s is a eyword

(ataType is one o" #any possible eywords "or the type


o" 'alue the 'ariable will contain

4ere is an exa#ple o" a 'ariable declaration,


(im int+ength as Integer
Chapter 3& Slide
7@

You might also like