Programming with Microsoft
Visual Basic 2012
Chapter 1: An Introduction to Visual Basic 2012
Lesson A Objectives
After studying Lesson A, you should be able to:
•
•
•
•
•
•
•
Start and customize Visual Studio 2012
Create a Visual Basic 2012 Windows application
Manage the windows in the IDE
Set the properties of an object
Restore a property to its default setting
Save a solution
Close and open an existing solution
Programming with Microsoft Visual Basic 2012
2
The Splash Screen Application
Figure 1-1 Splash screen for the Red Tree Inn
Programming with Microsoft Visual Basic 2012
3
The Splash Screen Application
(cont.)
• To start Visual Studio Professional 2012:
– Click Start and point to All Programs
– Select Microsoft Visual
Studio 2012
– Click Visual Basic
Development Settings
– Click Start Visual Studio
– Click WINDOW on the menu
bar
– Click Reset Window Layout
– Click Yes at the prompt to
Figure 1-2 Microsoft Visual Studio Professional 2012 startup screen
restore the default layout
– You can also select a different window
layout using TOOLS, Settings on the menu bar
Programming with Microsoft Visual Basic 2012
4
The Splash Screen Application
(cont.)
• To create a Visual Basic 2012 Windows application:
– Click FILE on the menu bar,
and then click New Project
– Enter Splash Project in
the Name Box
– Locate a folder to store the
project
– Select Create Directory for
Solution
Programming with Microsoft Visual Basic 2012
Figure 1-3 Options dialog box
5
The Splash Screen Application
(cont.)
Figure 1-4 Completed New Project dialog box in Visual Studio Professional 2012
Programming with Microsoft Visual Basic 2012
6
The Splash Screen Application
(cont.)
Figure 1-5 Solution and Visual Basic project
Programming with Microsoft Visual Basic 2012
7
The Splash Screen Application
(cont.)
Managing the Windows in the IDE
• To open a window:
– Click VIEW on the menu bar, and then click Properties
Window
– Or mouseover the Solution Explorer tab if the window is
auto-hidden
• To close a window:
– Use the Close button on the window’s title bar
• To minimize a window to a tab:
– Click the Auto Hide (push-pin) button on the Solution
Explorer window
Programming with Microsoft Visual Basic 2012
8
The Splash Screen Application
(cont.)
Figure 1-6 Current status of the windows in the IDE
Programming with Microsoft Visual Basic 2012
9
The Splash Screen Application
(cont.)
The Windows Form Designer
Window
• Used to create a graphical
user interface (GUI)
• Windows Form object
– Create a UI by adding objects
• Buttons
• Text boxes
– The title bar contains a default
caption
Figure 1-7 Windows Form Designer window
• Also minimize, maximize, and close buttons
Programming with Microsoft Visual Basic 2012
10
The Splash Screen Application
The Solution Explorer Window
• Displays a list of projects contained
in the current solution and items
contained in each project
• Source file
(cont.)
Figure 1-8 Solution Explorer window
– A file containing program instructions (code)
• Form file
– Contains code associated with a form
– The first form’s code is stored in Form1.vb
– The second form’s code is stored in Form2.vb
Programming with Microsoft Visual Basic 2012
11
The Splash Screen Application
(cont.)
The Properties Window
• Properties
– Attributes of objects
that determine
appearance and
behavior
Figure 1-9 Properties window
• Object box
– Contains the name of the selected object
• Properties list
– Helps identify and access properties
• Settings box
– Shows the current values of properties
Programming with Microsoft Visual Basic 2012
12
Properties of a Windows Form
• System.Windows.Forms
is the namespace that contains
the Form class definition
• A class definition is a block of
code that specifies an object’s
appearance and behavior
• The period that separates each
word in
System.Windows.Forms.Form
is called the dot member
access operator
Programming with Microsoft Visual Basic 2012
Figure 1-11 Properties window showing a partial
listing of the form’s properties
13
Properties of a Windows Form
(cont.)
The Name Property
• Windows forms have a Name property
– Use it to refer to the form in the code
– Naming conventions are used for clarity
• Hungarian notation
– frmSplash (“frm” indicates a form, and “Splash”
is the form’s purpose)
• Camel case
– Enter ID characters in lowercase and then capitalize
the first letter of each subsequent word in the name
Programming with Microsoft Visual Basic 2012
14
Properties of a Windows Form
(cont.)
The Text Property
• The Text property controls the text displayed in the
form’s title bar
– The default text for the Text property of Form 1 is Form1
• Change the default name to something meaningful
– The Name property is used by the programmer
– The Text property is read by the user when the
application is running
Programming with Microsoft Visual Basic 2012
15
Properties of a Windows Form
(cont.)
The StartPosition Property
• The computer uses the form’s StartPosition property to
determine the form’s initial position on the screen
– Splash screens usually appear in the middle of the screen
• Choose CenterScreen for the StartPosition
Programming with Microsoft Visual Basic 2012
16
Properties of a Windows Form
(cont.)
The Font Property
• A font is the general shape of a character
• Font styles include regular, bold, and italic
• Font sizes (8, 10, 12, etc.) are in points, with 1 point
equaling 1/72 of an inch
• The recommended font for applications is Segoe
(pronounced SEE-go)
– This is Segoe UI, Size 18
Programming with Microsoft Visual Basic 2012
17
Properties of a Windows Form
(cont.)
The Size Property
• A form can be sized by dragging the sizing handles that
appear around it
• You also can size an object by selecting it and then
pressing and holding down the Shift key as you press
the up, down, right, or left arrow key on the keyboard
• In addition, you can set the object’s Size property,
which contains two numbers, measured in pixels:
– The first number represents the width of the form
– The second number represents the height of the form
Programming with Microsoft Visual Basic 2012
18
Setting and Restoring a Property’s Value
•
•
•
•
Click BackColor in the Properties
list and then click the list arrow
in the Settings box
Click the Custom tab and then
click a red square to change the
background color of the form to
red
Now, right-click BackColor in the
Properties list and then click
Reset on the context menu
The background color of the
form returns to its default
setting
Programming with Microsoft Visual Basic 2012
Figure 1-12 Status of the form in the IDE
19
Saving, Closing, Opening, and Exiting a
Solution
Saving a Solution
• An asterisk (*) appears on the designer tab whenever you
make a change to the form
• Save the solution by clicking FILE on the menu bar and then
clicking Save All
Closing the Current Solution
• Click FILE on the menu bar and then click Close Solution
• To exit Visual Studio 2012, click FILE on the menu bar and
then click Exit
Opening an Existing Solution
• Click FILE on the menu bar, and then locate and open the
solution from the list of filenames available
Programming with Microsoft Visual Basic 2012
20
Lesson A Summary
•
•
•
•
•
•
•
Starting Visual Studio
Configuring Visual Studio
Creating a Visual Basic 2012 Windows application
Resetting a window layout
Closing and opening windows
Auto-hiding a window
Setting property values (name, text, location, font, size,
background color)
• Saving, closing, and opening solutions
• Exiting Visual Studio 2012
Programming with Microsoft Visual Basic 2012
21
Lesson B Objectives
After studying Lesson B, you should be able to:
•
•
•
•
•
•
•
•
•
Add a control to a form
Set the properties of a label, picture box, and button control
Select multiple controls
Center controls on the form
Open the Project Designer window
Start and end an application
Enter code in the Code Editor window
Terminate an application using the Me.Close() instruction
Run the project’s executable file
Programming with Microsoft Visual Basic 2012
22
The Toolbox Window
• Class
– A pattern from which an object is created
• Object
– An instance with behaviors and attributes
• Toolbox window (or toolbox)
– Contains tools for designing a user interface
– Each tool represents a class
– Controls are instantiated using tools from the toolbox
• Controls
– GUI objects that appear on a form
Programming with Microsoft Visual Basic 2012
23
The Toolbox Window
(cont.)
Figure 1-13 Toolbox window showing the purpose of the Label tool
Programming with Microsoft Visual Basic 2012
24
The Label Tool
• Used to create a label control
– Displays text that cannot be edited at run time
• Name property
– A unique name for a control
• Text property
– Specifies the value that appears in a label control
• Location property
– Specifies the location of a control on a form
– Includes an X value and a Y value
Programming with Microsoft Visual Basic 2012
25
The Label Tool
(cont.)
Figure 1-15 Label control added to the form
Programming with Microsoft Visual Basic 2012
26
The Label Tool
(cont.)
Setting the Text Property
• Select the lblMsg control
–
–
–
–
Click text in the Properties list
Type A relaxing place to stay!
Press Enter
Verify that the AutoSize property is set to True
• Select the lblName control
– Click text in the Properties list
– Type Red Tree Inn
– Press Enter
Programming with Microsoft Visual Basic 2012
27
The Label Tool
(cont.)
Setting the Location Property
• Select the lblMsg control
–
–
–
–
Click Location in the Properties list
Expand the Location property by clicking the plus box
Type 315, 175
Press Enter
• Select the lblName control
–
–
–
–
Click Location in the Properties list
Expand the Location property by clicking the plus box
Type 315, 130
Press Enter
Programming with Microsoft Visual Basic 2012
28
Changing a Property For Multiple
Controls
• A control inherits the font attribute of the form
– You may want to change the default values
• To simultaneously modify similar controls:
– Click one control and then press and hold the Ctrl key
– Click other similar control(s) in the form, or click and drag
to group the controls
– Set the property value in the Properties list
Programming with Microsoft Visual Basic 2012
29
Using the Format Menu
• The Format menu provides options to manipulate form
controls
– Align: Aligns selected controls by left, right, top, or
bottom borders
– Make Same Size: Makes the width and/or height of
selected controls uniform
– Center in Form: Centers one or more controls
• Select as the first control the one whose size or location
you want to match
– Called the reference control
Programming with Microsoft Visual Basic 2012
30
The PictureBox Tool
• Creates a picture box control
– Displays an image on a form
– Task box: Lists tasks
associated with a picture box
Figure 1-17 Open task list for a picture box
• Use the task box to import
an image into a project
• A project’s resource file
contains imported images
Figure 1-18 Completed Select Resource dialog box
Programming with Microsoft Visual Basic 2012
31
The PictureBox Tool
(cont.)
Figure 1-19 Picture boxes added to the form
Programming with Microsoft Visual Basic 2012
32
The Button Tool
• Used to create a button control
– Used to perform an action when clicked
– Examples: OK and Cancel buttons
• Common means of exiting a Windows application:
– Exit option on the FILE menu
– Exit button
Programming with Microsoft Visual Basic 2012
33
Starting and Ending an Application
• Startup form
– Displays when an application is first started
• The Project Designer window specifies the startup form
or the executable code to be run
– Right-click My Project in the Solution Explorer window
and then click Open
– Or, click PROJECT on the menu bar and then click
Properties
• Application tab
– Specify startup form
Programming with Microsoft Visual Basic 2012
34
Starting and Ending an Application
•
Executable file
•
•
•
•
(cont.)
A file that can be run outside of the IDE
Its filename ends with .exe
It is automatically created when a Visual Basic
application is started
Two ways to start an application in the IDE:
•
•
Click DEBUG on the
menu bar and then
click Start Debugging
Press the F5 key
Figure 1-20 Application pane in the Project Designer window
Programming with Microsoft Visual Basic 2012
35
The Code Editor Window
• Events
– User actions such as double-clicking
• Event procedure
– A set of program instructions that are processed when an
event occurs
• Code Editor window
– Provides templates for creating event procedures
– To open it, right-click the form and then click View Code
Programming with Microsoft Visual Basic 2012
36
The Code Editor Window
(cont.)
Figure 1-22 Code Editor window opened in the IDE
Programming with Microsoft Visual Basic 2012
37
The Code Editor Window
(cont.)
• Class Name list box
– Lists names of objects included in the UI
• Method Name list box
– Lists appropriate events for the selected object
• To select a control’s event in the Code Editor window:
– Select the class name first
– Then select the method name
• Syntax
– The rules of a programming language
Programming with Microsoft Visual Basic 2012
38
The Code Editor Window
(cont.)
• OOP
– Object-oriented programming; events are clickable
• Procedure header
– The first line in the code template for an event procedure
• Procedure footer
– The last line in the code template for an event procedure
• Keyword
– A word that has a special meaning in a programming
language
– Color coded in the IDE
Programming with Microsoft Visual Basic 2012
39
The Code Editor Window
(cont.)
• Sub procedure
– A block of code that performs a specific task
• Keywords in a sub procedure block:
– Sub: Starts a sub procedure
– Private: Restricts the use of the procedure to the form
where it appears
– End Sub: Indicates the end of a sub procedure
• Event sub procedure names include the object name
and event name
– Example: btnExit.Click
Programming with Microsoft Visual Basic 2012
40
The Code Editor Window
(cont.)
The Me.Close() Instruction
• Method
– A predefined procedure
– Can be called (or invoked) when needed
• Me.Close() method
– Causes the current form to close
– If it is the only form in an application, the application terminates
• IntelliSense feature
– Provides appropriate selections as pop-up lists
– Type the first few letters of your choice
– Then use the arrow keys to select an item in the list
Programming with Microsoft Visual Basic 2012
41
The Code Editor Window
(cont.)
Figure 1-25 List displayed by the IntelliSense feature
Programming with Microsoft Visual Basic 2012
42
Lesson B Summary
• To add a control to a form, use a tool from the toolbox
• Use the label control for text that users cannot edit
• You can set the properties of multiple controls
simultaneously
• To start and stop an application in the IDE, use DEBUG
on the menu bar
• Use the Code Editor window to display an object’s event
procedures
• The Me.Close() method closes the current form at
run time
Programming with Microsoft Visual Basic 2012
43
Lesson C Objectives
After studying Lesson C, you should be able to:
•
•
•
•
•
•
Set the properties of a timer control
Delete a control from the form
Delete code from the Code Editor window
Code a timer control’s Tick event procedure
Prevent the user from sizing a form
Remove and/or disable a form’s Minimize, Maximize, and
Close buttons
• Print an application’s code and interface
Programming with Microsoft Visual Basic 2012
44
Using the Timer Tool
• Splash screens typically close automatically
– They do not usually contain an Exit button
– The timer control removes the splash screen
• Timer tool
– Used to create a timer control
• Timer control
– Processes code at regular time intervals
– Does not appear on the user interface at run time
• Interval property
– Specified in milliseconds
• Enabled property
– Allows the timer to run if set to True
Programming with Microsoft Visual Basic 2012
45
Using the Timer Tool
(cont.)
• Tick event
– Occurs each time an interval has elapsed
– Condition: The timer must be running (enabled)
• Add code to the Tick event procedure
– Code will be run when the Tick event occurs
• Component tray
– A special area below the form design area in the IDE
– Stores controls not visible in the user interface at run
time
Programming with Microsoft Visual Basic 2012
46
Using the Timer Tool
(cont.)
Figure 1-29 Timer control placed in the component tray
Programming with Microsoft Visual Basic 2012
47
Setting the FormBorderStyle Property
• FormBorderStyle property
– Determines the border style of a form
• Example settings:
– Sizable: Allows the user to change a form’s size by
dragging its borders at run time
– FixedSingle: A fixed, thin border line
– None: No border
Programming with Microsoft Visual Basic 2012
48
The MinimizeBox, MaximizeBox,
and ControlBox Properties
• MinimizeBox property
– Determines whether the Minimize button appears on the
form’s title bar
• MaximizeBox property
– Determines whether the Maximize button appears on
the form’s title bar
• ControlBox property
– Determines whether the title bar appears on the form
• Splash screens typically do not contain a title bar
– To remove a title bar, delete the contents of the form’s
Text property
Programming with Microsoft Visual Basic 2012
49
The MinimizeBox, MaximizeBox,
and ControlBox Properties
(cont.)
Figure 1-31 Completed splash screen
Programming with Microsoft Visual Basic 2012
50
Printing the Application’s Code
and Interface
• Printing the application’s code and user interface helps
you understand and maintain the application
• To print the code:
– Press Prnt Scrn on the keyboard
– Paste into Word or Paint and then print
• To print the interface:
– Click the Splash Form.vb tab to make the Code Editor
window the active window
– Click FILE on the menu bar, and then click Print to open
the Print dialog box
Programming with Microsoft Visual Basic 2012
51
Printing the Application’s Code
and Interface
(cont.)
Figure 1-32 Print dialog box
Programming with Microsoft Visual Basic 2012
52
Lesson C Summary
• The timer control processes code at specified intervals
• A form’s MinimizeBox and MaximizeBox properties
enable or disable the Minimize and Maximize buttons
on the form
• A form’s ControlBox property determines whether the
icon and control buttons appear in the title bar
• A form’s FormBorderStyle property controls the
appearance of the form’s border
• Print Visual Basic code from the Code Editor window
• Print the user interface from the designer window
Programming with Microsoft Visual Basic 2012
53
Programming with Microsoft
Visual Basic 2012
Chapter 2: Designing Applications
Previewing the Play It Again Movies
Application
• Play It Again Movies application
– Allows salespeople to enter the date and number of
DVDs and Blu-rays sold
– Calculates and displays the total number of discs ordered
and the total sales for the order
Figure 2-2 Completed sales receipt
Figure 2-3 Print preview window
Programming with Microsoft Visual Basic 2012
2
Lesson A Objectives
After studying Lesson A, you should be able to:
• Plan an object-oriented Windows application in Visual Basic
2012
• Complete a TOE (Task, Object, Event) chart
• Follow the Windows standards regarding the layout and
labeling of controls
Programming with Microsoft Visual Basic 2012
3
Creating an Object-Oriented
Application
Developing an application is like building a home
The role of the programmer is analogous to that of a
builder
Bugs are problems that affect application functions
Programming with Microsoft Visual Basic 2012
4
Creating an Object-Oriented
Application
(cont.)
Figure 2-4 Processes used by a builder and a programmer
Programming with Microsoft Visual Basic 2012
5
Planning an Object-Oriented
Application
• Actively involve the user in the planning phase
– The end product should closely match the user’s needs
and wants
• TOE chart
– Used to record tasks, objects, and events required for
the application
Figure 2-5 Steps for planning an OO application
Programming with Microsoft Visual Basic 2012
6
Planning an Object-Oriented
Application
(cont.)
Identifying the Application’s Tasks
• What information will the application need to display
on the screen and/or print on the printer?
• What information is entered into the user interface?
• What information will the application need to calculate
to produce the desired result?
• How will the user end the
application?
• Will previous information need
to be cleared from the screen
before new information is entered?
Figure 2-6 Sample of the store’s
current sales receipt
Programming with Microsoft Visual Basic 2012
7
Planning an Object-Oriented
Application
(cont.)
Figure 2-7 Tasks entered in a TOE chart
Programming with Microsoft Visual Basic 2012
8
Planning an Object-Oriented
Application
(cont.)
Identifying the Objects
• Assign each task to an object in the user interface
• Objects used here:
– Label control
• Displays information that the user should not change
– Button control
• Performs an action immediately after a Click event
– Text box
• Provides an area for the user to enter data
Programming with Microsoft Visual Basic 2012
9
Planning an Object-Oriented
Application
(cont.)
Figure 2-8 Tasks and objects entered in a TOE chart
Programming with Microsoft Visual Basic 2012
10
Planning an Object-Oriented
Application
(cont.)
Identifying the Events
• Determine which event (if any) must occur for an
object to carry out its assigned task
• Text boxes and label controls
– No special event is needed
• btnCalc, btnClear, and btnExit buttons
– Perform assigned tasks when clicked
Programming with Microsoft Visual Basic 2012
11
Planning an Object-Oriented
Application
(cont.)
Figure 2-9 Completed TOE chart ordered by task
Figure 2-10 Completed TOE chart ordered by object
Programming with Microsoft Visual Basic 2012
12
Planning an Object-Oriented
Application
(cont.)
Drawing a Sketch of the User Interface
• Follow Windows standards for designing the interface
• In Western countries, information flows either vertically
or horizontally
– Vertical arrangement:
• Information flows from top to bottom, with essential
information located in the first column
– Horizontal arrangement:
• Information flows from left to right, with essential
information placed in the first row
Programming with Microsoft Visual Basic 2012
13
Planning an Object-Oriented
Application
(cont.)
Figure 2-11 Vertical arrangement of the Play It
Again Movies application
Programming with Microsoft Visual Basic 2012
Figure 2-12 Horizontal arrangement of the Play It
Again Movies application
14
Planning an Object-Oriented
Application
(cont.)
• White space or containers may be used to group related
controls
• Containers
– Objects used to group related controls
– Examples: GroupBox, Panel, TableLayoutPanel
• Label controls that display output should have
meaningful names
– Example: “Total Sales” identifies the lblTotalSales label
• Identifying labels should end with a colon (:)
– Example: “Total Sales:”
Programming with Microsoft Visual Basic 2012
15
Planning an Object-Oriented
Application
(cont.)
• Sentence capitalization
– Only the first letter in the first word is capitalized
– Use for identifying labels
• Book title capitalization
– Capitalize the first letter of each word except articles,
conjunctions, and prepositions
– Use for button text
• Buttons should be aligned
– Also same height and width
• Group related controls close to each other
Programming with Microsoft Visual Basic 2012
16
Planning an Object-Oriented
Application
(cont.)
Programming with Microsoft Visual Basic 2012
17
Lesson A Summary
• Steps to create an OO application:
– Meet with the client
– Plan the application
• Identify needed tasks, objects, and events
• Identify information needed as input to produce the
desired result
–
–
–
–
Build the user interface
Code the application
Test and debug the application
Assemble the documentation
Programming with Microsoft Visual Basic 2012
18
Lesson B Objectives
After studying Lesson B, you should be able to:
• Build the user interface using your TOE chart and sketch
• Follow the Windows standards regarding the use of
graphics, fonts, and color
• Set a control’s BorderStyle, AutoSize, and TextAlign
properties
• Add a text box to a form
• Lock the controls on the form
• Assign access keys to controls
• Set the TabIndex property
Programming with Microsoft Visual Basic 2012
19
Building the User Interface
• Use the TOE chart and sketch as guides when building
the user interface
– Place appropriate controls on forms
– Set applicable properties of controls
• Features of the UI used in this lesson’s application:
– Information is arranged vertically
– Controls are aligned and appropriately labeled
• Try to create an interface that no one notices
Programming with Microsoft Visual Basic 2012
20
Building the User Interface
(cont.)
Figure 2-13 Partially completed interface for the Play It Again Movies application
Programming with Microsoft Visual Basic 2012
21
Building the User Interface
(cont.)
Including Graphics in the User Interface
• Graphics
– Icons or pictures added to an interface
– Used to emphasize or clarify a portion of the screen, or
for aesthetic purposes
• The human eye is attracted to pictures before text
– Include graphics sparingly
– Graphics for aesthetic use should be small and positioned
to avoid distracting the user
Programming with Microsoft Visual Basic 2012
22
Building the User Interface
(cont.)
Selecting Fonts for the Interface
• GUI DESIGN TIP: Selecting Font Types, Styles, and Sizes
– Use only one font type—typically Segoe UI (pronounced
“see-go”)—for all of the text in the interface
– Use no more than two different font sizes in the interface
– Avoid using italics and underlining because both font
styles make text difficult to read
– Limit the use of bold text to titles, headings, and key
items that you want to emphasize
Programming with Microsoft Visual Basic 2012
23
Building the User Interface
(cont.)
Adding Color to the Interface
• Build the interface using black, white, and gray; only add
color if you have a good reason to do so
• Use white, off-white, or light gray for the background; use
black for the text
• Never use a dark color for the background or a light color for
the text; a dark background is hard on the eyes, and lightcolored text can appear blurry
• Limit the number of colors in an interface to three, not
including white, black, and gray; the colors you choose
should complement each other
• Never use color as the only means of identifying an element
in the interface
Programming with Microsoft Visual Basic 2012
24
Building the User Interface
(cont.)
The BorderStyle, AutoSize, and TextAlign Properties
• The BorderStyle property can be set to None, FixedSingle, or
Fixed3D
– None is used for labels
– FixedSingle surrounds the control with a thin line
• Used for labels that display program output
– Fixed3D gives the control a three-dimensional appearance
• Used for text boxes
• The AutoSize property determines if a control automatically
sizes to fit its current contents
– Typically set to false for label controls that display program
output
• The TextAlign property can be set to nine different values
Programming with Microsoft Visual Basic 2012
25
Building the User Interface
(cont.)
Adding a Text Box Control to the Form
• A text box control provides an area for data entry
• Use the TextBox tool to add a text box control
– Make all text boxes
the same size and align
them using snap lines
– Blue snap lines are used
for vertical alignment
– Pink snap lines are used
for horizontal alignment
Programming with Microsoft Visual Basic 2012
Figure 2-14 Snap lines shown in the interface
26
Locking the Controls on a Form
• Lock controls after they are properly placed to avoid
inadvertently moving them
• A locked control is identified by a small lock
• To lock controls:
– Right-click the form (or any control on the form)
– Click Lock Controls on the FORMAT menu
• Follow the same procedure to unlock controls
Programming with Microsoft Visual Basic 2012
27
Assigning Access Keys
• Access key
– Enables an object to be selected using the keyboard
– Key combination: Alt key + letter or number
• Each access key must be unique
• Shown in the interface as an underlined letter
• To assign an access key, include an ampersand (&) in
the control’s caption
– Example: “&Calculate Order” assigns ‘C’ to the button
Programming with Microsoft Visual Basic 2012
28
Assigning Access Keys
(cont.)
• Reasons to assign access keys:
– Allow the user to work even if the mouse does not
– Allow fast typists to keep their hands on the keyboard
– Allow people with disabilities that prevent them from
using a mouse to be able to use an application
• Follow Windows standards for assigning commonly
used access keys
Programming with Microsoft Visual Basic 2012
29
Controlling the Tab Order
• Focus
– The state in which a control is ready to accept user input
or action
– Pressing the Tab key or access key shifts the focus
• TabIndex property
– A number representing the order in which a control will
receive the focus when the user presses the Tab key
– A control with a TabIndex of 0 receives the focus first
• Set TabIndex using the Properties window or the Tab
Order option on the VIEW menu
– Make a list of objects to determine the proper order
Programming with Microsoft Visual Basic 2012
30
Controlling the Tab Order
(cont.)
Figure 2-16 TabIndex boxes showing the correct
TabIndex values
Figure 2-15 List of controls and TabIndex values
Programming with Microsoft Visual Basic 2012
31
Lesson B Summary
• Use appropriate graphics, fonts, and colors in an
interface
• Set the BorderStyle, AutoSize, and TextAlign properties
• To lock/unlock controls on a form, use the Lock Controls
option on the FORMAT menu
• To assign an access key to a control, type an ampersand
(&) in the Text property of the control or identifying
label
• To set the tab order, set the TabIndex property to a
number that represents the order in which you want
the control to receive the focus
Programming with Microsoft Visual Basic 2012
32
Lesson C Objectives
After studying Lesson C, you should be able to:
•
•
•
•
•
•
•
•
•
Code an application using its TOE chart
Plan an object’s code using either pseudocode or a flowchart
Write an assignment statement
Send the focus to a control during run time
Include internal documentation in the code
Write arithmetic expressions
Use the Val and Format functions
Print an interface from code
Locate and correct syntax errors
Programming with Microsoft Visual Basic 2012
33
Coding the Application
• Code
– Instructions added to an application
• Coding is done after planning and building the interface
• TOE charts show which objects and events need to be
coded
• Play It Again Movies application code requirements:
– Four buttons associated with Click events
Programming with Microsoft Visual Basic 2012
34
Coding the Application
(cont.)
Figure 2-21 Play It Again Movies user
interface from Lesson B
Figure 2-22 TOE chart (ordered by object) for Play It Again Movies
Programming with Microsoft Visual Basic 2012
35
Coding the Application
(cont.)
Using Pseudocode to Plan a Procedure
• Pseudocode
– Short phrases used to describe the steps a procedure
must take to accomplish its goal
– Travel directions are a type of pseudocode
• btnCalc Click event procedure
– Calculates the total number of phones ordered and the
total price
– Displays the results at run time
Programming with Microsoft Visual Basic 2012
36
Coding the Application
(cont.)
Figure 2-23 Pseudocode for the Play It Again Movies application
Programming with Microsoft Visual Basic 2012
37
Coding the Application
(cont.)
Using a Flowchart to Plan a Procedure
• A flowchart shows program logic using standardized
symbols
–
–
–
–
Oval: Start/stop symbol
Rectangle: Process symbol; represents a task
Parallelogram: Input/output symbol
Flowlines connect the symbols
• Flowcharts depict the same logic as pseudocode
Programming with Microsoft Visual Basic 2012
38
Coding the Application
(cont.)
Figure 2-24 Flowcharts for Play It Again Movies
Programming with Microsoft Visual Basic 2012
39
Coding the btnClear Control’s Click
Event Procedure
• btnClear control’s task
– Clear the screen for the next order
• String
– Zero or more characters enclosed in quotation marks ("")
• Zero-length string (or empty string)
– A pair of quotation marks with nothing between them
("")
• Two ways to remove the control contents at run time:
– Assign a zero-length string to the control’s Text property
– Assign String.Empty to the control’s Text property
Programming with Microsoft Visual Basic 2012
40
Coding the btnClear Control’s Click
Event Procedure
(cont.)
Assigning a Value to a Property During Run Time
• Assignment statement
– An instruction assigning a value to an object at run time
• Syntax: object.property = expression
– object and property are the object and property names
– expression contains the value to be assigned
• Assignment operator (=)
– Assigns the value on the
right side to the object
on the left side
Programming with Microsoft Visual Basic 2012
Figure 2-27 First assignment statement entered in the procedure
41
Coding the btnClear Control’s Click
Event Procedure
(cont.)
Using the Focus Method
• Allows you to move the focus to a specified control
during run time
• Syntax: object.Focus()
– object is the name of the control that receives the focus
Programming with Microsoft Visual Basic 2012
42
Coding the btnClear Control’s Click
Event Procedure
(cont.)
Internally Documenting the Program Code
• Comments
– Internal documentation in a program
– Used by programmers to document a procedure’s
purpose or explain sections of code
– Help make code readable
• To create a comment, place an apostrophe (’) before a
statement
– The computer ignores all characters after the apostrophe
for the rest of the line
• Comments are color-coded in the IDE
Programming with Microsoft Visual Basic 2012
43
Coding the btnClear Control’s Click
Event Procedure
(cont.)
Figure 2-28 btnClear control’s Click event procedure
Programming with Microsoft Visual Basic 2012
Figure 2-29 Comments entered in the General Declarations
section
44
Coding the btnPrint Control’s Click
Event Procedure
Figure 2-30 Syntax and examples of printing the interface from code
Programming with Microsoft Visual Basic 2012
45
Coding the btnPrint Control’s Click
Event Procedure
(cont.)
Figure 2-32 Print preview window
Figure 2-33 Completed Click event procedure for the btnPrint control
Programming with Microsoft Visual Basic 2012
46
Writing Arithmetic Expressions
Figure 2-34 Most commonly used arithmetic operators
• Order of operations:
– PEMDAS (Parentheses, Exponents, Multiplication, Division,
Addition, Subtraction)
• Integer division operator
– Returns a whole number
• Modulus operator
– Returns the remainder of the division
Programming with Microsoft Visual Basic 2012
47
Writing Arithmetic Expressions
(cont.)
Figure 2-36 Expressions containing more than one operator having the same precedence
Programming with Microsoft Visual Basic 2012
48
Coding the btnCalc Control’s Click
Event Procedure
Figure 2-37 Illustration of the total discs sold calculation
Figure 2-38 Illustration of the total sales calculation
Programming with Microsoft Visual Basic 2012
49
Coding the btnCalc Control’s Click
Event Procedure
(cont.)
Figure 2-39 Interface showing the incorrect results
of the calculations
The Val Function
• A function is a predefined
procedure that performs a task
and returns a value
• Val temporarily converts a
string to a number and returns
the number
• Syntax: Val(string)
• You can use Val to correct
calculations in the btnCalc
control’s Click event procedure
Programming with Microsoft Visual Basic 2012
50
Coding the btnCalc Control’s Click
Event Procedure
(cont.)
Figure 2-40 Syntax and examples of the Val function
Figure 2-41 Val function entered in the assignment statements
Programming with Microsoft Visual Basic 2012
51
Coding the btnCalc Control’s Click
Event Procedure
(cont.)
The Format Function
• Improves the appearance of numbers
• Syntax: Format(expression, style)
– expression: Specifies the number, date, time, or string to
format
– style: A predefined or user-defined format style
• Currency: Example of a format style that displays a
number with a dollar sign and two decimal places
Programming with Microsoft Visual Basic 2012
52
Coding the btnCalc Control’s Click
Event Procedure
(cont.)
Figure 2-43 Format function’s syntax and some of the predefined format styles
Programming with Microsoft Visual Basic 2012
53
Coding the btnCalc Control’s Click
Event Procedure
(cont.)
Figure 2-44 Format function entered in the procedure
Figure 2-45 Formatted total sales amount shown in the interface
Programming with Microsoft Visual Basic 2012
54
Testing and Debugging the
Application
• Test an application using some sample data
– Use both valid and invalid data
• Valid data
– Data that the application is expecting
• Invalid data
– Data that the application is not expecting
• Debugging
– The process of locating and correcting errors in a program
– Errors can be related to either syntax or logic
Programming with Microsoft Visual Basic 2012
55
Testing and Debugging the
Application
(cont.)
• Syntax error
– Occurs when a rule of a programming language is broken
– Typos
• Logic error
– Occurs when the syntax is correct, but the outcome is not
what was desired
– Causes may include missing instructions, instructions out of
order, or the wrong type of instruction
• Run time error
– Occurs when an application is running and the application
stops
Programming with Microsoft Visual Basic 2012
56
Testing and Debugging the
Application
(cont.)
Figure 2-46 Suggestion for fixing the error
Figure 2-47 Message dialog box
Programming with Microsoft Visual Basic 2012
57
Testing and Debugging the
Application
(cont.)
Figure 2-48 Error List window in the IDE
Programming with Microsoft Visual Basic 2012
58
Assembling the Documentation
• Important documentation
– Planning tools
– Printout of the application’s interface and code
• Your planning tools include:
– TOE chart
– Sketch of interface
– Flowcharts and/or pseudocode
Programming with Microsoft Visual Basic 2012
59
Assembling the Documentation
(cont.)
Figure 2-49 Play It Again Movies code
Programming with Microsoft Visual Basic 2012
60
Lesson C Summary
• Use pseudocode or a flowchart to plan an object’s code
• To assign a value to the property of an object while an
application is running, use an assignment statement
with the syntax object.property = expression
• To create comments, begin the comment text with an
apostrophe (’)
• Use the integer division operator (\) to divide and return
an integer result
Programming with Microsoft Visual Basic 2012
61
Lesson C Summary
(cont.)
• Use the modulus operator to divide two numbers and
return the remainder
• Use the PrintForm tool to print the form
• To temporarily convert a string to a number, use the Val
function
• Use the Format function to improve the appearance of
numbers
• The application should be tested with both valid and
invalid data
Programming with Microsoft Visual Basic 2012
62
Programming with Microsoft
Visual Basic 2012
Chapter 3: Using Variables and Constants
Previewing the Modified Play It Again
Movies Application
• Play It Again Movies application
– New screen for salesclerk’s name
– Sales tax added to order
Figure 3-1 Name Entry dialog box
Figure 3-2 Completed sales receipt
Programming with Microsoft Visual Basic 2012
2
Lesson A Objectives
After studying Lesson A, you should be able to:
• Declare variables and named constants
• Assign data to an existing variable
• Convert string data to a numeric data type using the
TryParse method
• Convert numeric data to a different data type using the
Convert class methods
• Explain the scope and lifetime of variables and named
constants
• Explain the purpose of Option Explicit, Option Infer, and
Option Strict
Programming with Microsoft Visual Basic 2012
3
Using Variables to Store Information
• Controls and variables temporarily store data
• Variable
– A temporary storage location in main memory
– Specified by data type, name, scope, and lifetime
• Reasons to use variables
– To hold information that is not stored in a control on
the form
– To allow for more precise treatment of numeric data
– To enable code to run more efficiently
Programming with Microsoft Visual Basic 2012
4
Using Variables to Store Information
(cont.)
Selecting a Data Type for a Variable
• Data type
– Specifies the type of data a variable can store
– Provides a class template for creating variables
• Unicode
– A universal coding scheme for characters
– Assigns a unique numeric value to each character in the
written languages of the world
Programming with Microsoft Visual Basic 2012
5
Using Variables to Store Information
(cont.)
Figure 3-3 Basic data types in Visual Basic
Programming with Microsoft Visual Basic 2012
6
Using Variables to Store Information
(cont.)
• The textbook uses:
– The Integer data type for all integers
– Either the Decimal or Double data type for numbers
containing decimal places or numbers used in
calculations
– The String data type for text or numbers not used in
calculations
– The Boolean data type for Boolean values
Programming with Microsoft Visual Basic 2012
7
Using Variables to Store Information
(cont.)
Selecting a Name for a Variable
– Names must begin
with a letter or
underscore
Figure 3-4 Three-character IDs and examples
– Names can contain only letters, numbers, or underscores
• No punctuation, special characters, or spaces are allowed
– The recommended length for a name variable is 32
characters
– Variable names cannot be reserved words (such as Sub or
Double)
Programming with Microsoft Visual Basic 2012
8
Using Variables to Store Information
(cont.)
•
•
Valid names:
– intFeb_Income
– decSales2014
– dblEastRegion
– strName
Invalid Names:
– 4thQuarter
• The name must begin with a letter or underscore
– dblWest Region
• The name cannot contain a space
– strFirst.Name
• The name cannot contain punctuation
– decSales$East
• The name cannot contain a special character
Programming with Microsoft Visual Basic 2012
9
Using Variables to Store Information
(cont.)
Declaring a Variable
• Declaration statement
– Used to declare (create) a variable and reserve space in
memory for it
• If no initial value is given to a variable when declaring it,
the computer stores a default value
–
–
–
–
Numeric variables are set to 0
Boolean variables are set to False
Object and String variables are set to Nothing
Date variables are set to 1/1/0001 12:00:00AM
Programming with Microsoft Visual Basic 2012
10
Using Variables to Store Information
(cont.)
Figure 3-6 Syntax and examples of a variable declaration statement
Programming with Microsoft Visual Basic 2012
11
Assigning Data to an Existing Variable
• Assignment statement
– Assigns a value to a variable at run time
– Syntax: variablename = expression
– An expression may include literal constants, object
properties, variables, keywords, and arithmetic operators
• Literal constant
– A data item whose value does not change while the
application is running
– Example: The string “Mary”
• Literal type character
– Forces a literal constant to change its data type
Programming with Microsoft Visual Basic 2012
12
Assigning Data to an Existing Variable
(cont.)
Figure 3-7 Syntax and examples of assigning a value to a variable during run time
Programming with Microsoft Visual Basic 2012
13
Assigning Data to an Existing Variable
(cont.)
The TryParse Method
• Converts a string to a number
• Is preferred over Val
– Allows the programmer to specify the data type
– Val only returns a Double number
• Arguments
– dataType:
– String:
– Variable:
A numeric data type, such as Integer
A string to be converted
A variable that receives the numeric value
Programming with Microsoft Visual Basic 2012
14
Assigning Data to an Existing Variable
(cont.)
Figure 3-9 Basic syntax and examples of the TryParse method
Programming with Microsoft Visual Basic 2012
15
Assigning Data to an Existing Variable
(cont.)
Figure 3-10 Results of the TryParse method for the Double, Decimal and Integer data types
Programming with Microsoft Visual Basic 2012
16
Assigning Data to an Existing Variable
(cont.)
The Convert Class
• Can be used to convert a number from one type to
another
• Methods include ToDecimal, ToDouble, ToInt32, and
ToString
• TryParse is recommended for converting strings to
numeric data types
– Will not produce an error if the conversion fails
Programming with Microsoft Visual Basic 2012
17
Assigning Data to an Existing Variable
(cont.)
Figure 3-11 Syntax and examples of the Convert class methods
Programming with Microsoft Visual Basic 2012
18
The Scope and Lifetime of a Variable
• Scope
– Indicates where a variable can be used
• Lifetime
– How long a variable remains in memory
• Scope and lifetime are determined by where a variable is
declared: either the General Declarations section or the
form’s Declaration section
• Three types of scope:
– Class: The variable can be used by all procedures in a form
– Procedure: The variable can be used within a procedure
– Block: The variable can be used within a specific code block
Programming with Microsoft Visual Basic 2012
19
The Scope and Lifetime of a Variable
(cont.)
• Variables with Procedure Scope
–
–
–
–
Can be used only by that procedure
Declared at the beginning of the procedure
Removed from memory when the procedure ends
Declared using the Dim keyword
• Most variables used in this course will be procedurelevel variables
Figure 3-12 User Interface for the Discount Calculator application
Programming with Microsoft Visual Basic 2012
20
The Scope and Lifetime of a Variable
(cont.)
Figure 3-13 Click event procedures using procedure-level variables
Programming with Microsoft Visual Basic 2012
21
The Scope and Lifetime of a Variable
(cont.)
Figure 3-14 Code Editor window for the Discount Calculator application
Programming with Microsoft Visual Basic 2012
22
The Scope and Lifetime of a Variable
(cont.)
• Variables with Class Scope
–
–
–
–
Can be used by all procedures in the form
Declared in the form’s Declarations section
Will remain in memory until the application ends
Declared using the Private keyword
Figure 3-16 User Interface for the Total Scores application
Programming with Microsoft Visual Basic 2012
23
The Scope and Lifetime of a Variable
(cont.)
Figure 3-17 Total Scores application code using a class-level variable
Programming with Microsoft Visual Basic 2012
24
Static Variables
• Static variable
– A procedure-level variable with an extended lifetime
• Remains in memory between procedure calls
• Retains its value even when the procedure ends
– Static keyword
• Used to declare a static variable
– Static variables act like class-level variables but have
narrower scope
• They can only be used by the procedure in which they are
declared
Programming with Microsoft Visual Basic 2012
25
Static Variables
(cont.)
Figure 3-19 Total Scores application’s code using a static variable
Programming with Microsoft Visual Basic 2012
26
Named Constants
• Named constant
– A memory location inside the computer whose contents
cannot be changed at run time
• Const statement
– Creates named constant
– Stores value of expression in a named constant
– expression: Can be a literal constant, another named
constant, or an arithmetic operator
– Cannot contain a variable or method
Programming with Microsoft Visual Basic 2012
27
Named Constants
(cont.)
Figure 3-20 Syntax and examples of the Const statement
Programming with Microsoft Visual Basic 2012
28
Named Constants
(cont.)
Figure 3-21 User interface for the Area Calculator application
Figure 3-22 Calculate Area button’s Click event procedure
Programming with Microsoft Visual Basic 2012
29
Option Statements
Option Explicit and Option Infer
• Prevent you from using undeclared variables
Option Strict
• Implicit type conversion
– Converts the right-side value to the data type on the left side
• Promotion
– Data is converted to a greater precision number (e.g., Integer to Decimal)
• Demotion
– Data is truncated (e.g., Decimal to Integer)
– Data loss can occur when demotion occurs
• Infer
– Ensures that every variable is declared with a data type
Programming with Microsoft Visual Basic 2012
30
Option Statements
(cont.)
Option Strict (cont.)
– Disallows implicit conversions
– Type conversion rules are applied when this option is on
Figure 3-24 Rules and examples of type conversions
Programming with Microsoft Visual Basic 2012
31
Option Statements
(cont.)
Figure 3-25 Option statements entered in the General Declarations section
Programming with Microsoft Visual Basic 2012
32
Lesson A Summary
•
•
•
•
•
•
Declare a variable using {Dim | Private | Static}
An assignment statement assigns a value to a variable
Three levels of scope: block, procedure, class
The TryParse method converts strings to numeric data
Use Const to declare a named constant
Avoid programming errors by using Option Explicit On,
Option Infer Off, and Option Strict On
Programming with Microsoft Visual Basic 2012
33
Lesson B Objectives
After studying Lesson B, you should be able to:
• Include procedure-level and class-level variables in an
application
• Concatenate strings
• Get user input using the InputBox function
• Include the ControlChars.NewLine constant in code
• Designate the default button for a form
• Format numbers using the ToString method
Programming with Microsoft Visual Basic 2012
34
Modifying the Play It Again Movies
Application
•
Modifications needed
– Calculate and display
the sales tax
– Display salesperson’s
name
•
•
Revise the TOE chart to
reflect the new tasks
You must modify btnCalc
button’s Click event and
the form’s Load event
Programming with Microsoft Visual Basic 2012
Figure 3-26 Revised TOE chart for the Play It Again Movies application
35
Modifying the Play It Again Movies
Application
(cont.)
Figure 3-27 Modified user interface for the Play It Again Movies application
Programming with Microsoft Visual Basic 2012
36
Modifying the Calculate Button’s Code
• General strategy
– Remove the existing code from the Click event procedure
– Recode the procedure using variables in equations
• Use the Option Explicit On statement
– Enforces full variable declaration
• Use the Option Infer Off statement
– Ensures that variables are declared with data types
• Use the Option Strict On statement
– Suppresses implicit type conversions
Programming with Microsoft Visual Basic 2012
37
Modifying the Calculate Button’s Code
(cont.)
Figure 3-28 A jagged blue line indicates a syntax error
Figure 3-29 Lines to delete from the procedure
Programming with Microsoft Visual Basic 2012
38
Modifying the Calculate Button’s Code
(cont.)
Figure 3-30 Revised pseudocode and flowchart for the btnCalc control’s Click event procedure
Programming with Microsoft Visual Basic 2012
39
Modifying the Calculate Button’s Code
(cont.)
Figure 3-31 List of named constants and variables
Figure 3-32 Const and Dim statements entered in the procedure
Programming with Microsoft Visual Basic 2012
40
Modifying the Calculate Button’s Code
(cont.)
Figure 3-34 Calculated amounts shown in the interface
Figure 3-33 Code entered in the btnCalc control’s Click event procedure
Programming with Microsoft Visual Basic 2012
41
Concatenating Strings
• Concatenate
– To connect strings together
• Concatenation operator
– The ampersand (&)
– Include a space before and after the ampersand
• Numeric values used with the & operator are converted to strings
Figure 3-35 Examples of string concatenation
Programming with Microsoft Visual Basic 2012
42
Concatenating Strings
(cont.)
Figure 3-36 String concatenation included in the assignment
statement
Figure 3-37 Concatenated strings displayed in the
lblMessage control
Programming with Microsoft Visual Basic 2012
43
The InputBox Function
InputBox function
– Displays an input dialog box and retrieves user input
• Arguments
– prompt: Contains the message to display inside the dialog
box
– title: Controls the text that appears in the dialog box’s title
bar
– defaultResponse: Controls the text that appears in the
input field
• The returned value most often assigned to String variable
• The syntax is shown in Figure 3-39 on the next slide
Programming with Microsoft Visual Basic 2012
44
The InputBox Function
(cont.)
Figure 3-38 Example of an input dialog box
Figure 3-39 Basic syntax and examples of the InputBox function
Programming with Microsoft Visual Basic 2012
45
The InputBox Function
(cont.)
Figure 3-42 Dialog box created by the InputBox function
Figure 3-40 Class-level variable declared in the form’s
Declarations section
Figure 3-41 frmMain Load event procedure
Programming with Microsoft Visual Basic 2012
46
The ControlChars.Newline Constant
ControlChars.NewLine constant
– Advances the insertion point to the next line in a control
– Also used to advance the insertion point in file or on the
printer
• To use, type ControlChars.NewLine in the
appropriate location
– Can be used with string concatenation
• Line continuation character (_)
– Used to break up a long line of code into two or more
lines
Programming with Microsoft Visual Basic 2012
47
The ControlChars.Newline Constant
(cont.)
Figure 3-43 Modified assignment statement
Figure 3-45 Salesclerk’s name shown on the sales receipt
Programming with Microsoft Visual Basic 2012
48
Designating a Default Button
Default button
– Activated by pressing the Enter key
– Not required to have the focus
– Only one per form
• The default button should be the button used most
often by the user
– Except if button’s task is destructive and irreversible,
such as deleting data
• Set the form’s AcceptButton property to the button
name
Programming with Microsoft Visual Basic 2012
49
Using the ToString Method to Format
Numbers
• Formatting
– Specifying decimal places and special characters to
display
• The ToString method replaces the Format function
• Syntax: variablename.ToString(formatString)
– variablename: The name of a numeric variable
– formatString: The string specifying the format you want
to use
• formatString must take the form Axx, where A is the
format specifier and xx is the precision specifier
Programming with Microsoft Visual Basic 2012
50
Using the ToString Method to Format
Numbers
(Cont.)
Figure 3-46 Syntax and examples of the ToString method
Programming with Microsoft Visual Basic 2012
51
Using the ToString Method to Format
Numbers
(Cont.)
Figure 3-48 Play It Again Movies application’s code at the end of Lesson B
(continues…)
Programming with Microsoft Visual Basic 2012
52
Using the ToString Method to Format
Numbers
(Cont.)
Figure 3-48 Play It Again Movies application’s code at the end of Lesson B
Programming with Microsoft Visual Basic 2012
53
Lesson B Summary
• The concatenation operator (&) is used to link strings
• The InputBox function displays an interactive dialog box
• Use ControlChars.NewLine to move the insertion point
to a new line
• Set the default button in the form’s AcceptButton
property
• The ToString method formats a number for string
output
Programming with Microsoft Visual Basic 2012
54
Lesson C Objectives
After studying Lesson C, you should be able to:
• Include a static variable in code
• Code the TextChanged event procedure
• Create a procedure that handles more than one event
Programming with Microsoft Visual Basic 2012
55
Modifying the Load and Click Event
Procedures
Figure 3-51 Revised TOE Chart for the Play It Again Movies application in Lesson C
Programming with Microsoft Visual Basic 2012
56
Modifying the Load and Click Event
Procedures
(Cont.)
Figure 3-52 Revised pseudocode for the Calculate button in Lesson C
Programming with Microsoft Visual Basic 2012
57
Coding the TextChanged Event Procedure
• TextChanged event
– Occurs when the Text property value of a control changes
• Can occur when:
– The user enters data into the control
– The code assigns data to the control’s Text property
• Example: A change is made to the number of items
ordered
Programming with Microsoft Visual Basic 2012
58
Coding the TextChanged Event Procedure
(Cont.)
Associating a Procedure with Different Objects and Events
• Handles clause
– Appears in an event procedure’s header
– Indicates the object and event associated with the
procedure
• You can associate an event procedure with more than one
object and/or event
– In the Handles section of procedure header, list each object
and event separated by commas
– Procedure names are entered in Pascal case
• Capitalize the first letter in the name and the first letter of
each subsequent word in the name
Programming with Microsoft Visual Basic 2012
59
Coding the TextChanged Event Procedure
(cont.)
Figure 3-53 TextChanged event procedure associated with the txtDvds control
Figure 3-54 Completed ClearLabels procedure
Programming with Microsoft Visual Basic 2012
60
Lesson C Summary
• The TextChanged event procedure responds to a change
in the value of a control’s Text Property
• The Handles clause determines which objects and
events are associated with the event procedure
• To create a procedure for more than one object or
event, list each object and event after the Handles
keyword
Programming with Microsoft Visual Basic 2012
61
Purchase answer to see full
attachment