100% found this document useful (1 vote)
34 views

Programming With Microsoft Visual Basic 2012 6th Edition Zak instant download

The document provides information about the 6th edition of 'Programming with Microsoft Visual Basic 2012' by Diane Zak, including links to download the book and other related resources. It outlines the content structure, including chapters on various programming concepts and techniques. Additionally, it includes copyright information and publishing details from Cengage Learning.

Uploaded by

tzachiruig
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
34 views

Programming With Microsoft Visual Basic 2012 6th Edition Zak instant download

The document provides information about the 6th edition of 'Programming with Microsoft Visual Basic 2012' by Diane Zak, including links to download the book and other related resources. It outlines the content structure, including chapters on various programming concepts and techniques. Additionally, it includes copyright information and publishing details from Cengage Learning.

Uploaded by

tzachiruig
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 80

Programming With Microsoft Visual Basic 2012 6th

Edition Zak download

https://ebookbell.com/product/programming-with-microsoft-visual-
basic-2012-6th-edition-zak-55132386

Explore and download more ebooks at ebookbell.com


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

Clearly Visual Basic Programming With Microsoft Visual Basic 2012 3rd
Edition Diane Zak

https://ebookbell.com/product/clearly-visual-basic-programming-with-
microsoft-visual-basic-2012-3rd-edition-diane-zak-4178308

Programming Microsoft Windows With Microsoft Visual Basic Net Core


Reference Prodeveloper Illustrated Edition Petzold

https://ebookbell.com/product/programming-microsoft-windows-with-
microsoft-visual-basic-net-core-reference-prodeveloper-illustrated-
edition-petzold-1273112

Programming Microsoft Sql Server 2000 With Microsoft Visual Basic Net
1st Edition Dobson

https://ebookbell.com/product/programming-microsoft-sql-
server-2000-with-microsoft-visual-basic-net-1st-edition-
dobson-55141988

Programming With Microsoft Visual Basic 2010 Vbnet Programming 5th


Edition Zak

https://ebookbell.com/product/programming-with-microsoft-visual-
basic-2010-vbnet-programming-5th-edition-zak-55139980
Programming With Microsoft Visual Basic 2015 7th Edition Diane Zak

https://ebookbell.com/product/programming-with-microsoft-visual-
basic-2015-7th-edition-diane-zak-5312274

Programming With Microsoft Visual Basic 2017 Mindtap Course List 8th
Edition Zak

https://ebookbell.com/product/programming-with-microsoft-visual-
basic-2017-mindtap-course-list-8th-edition-zak-55132312

Clearly Visual Basic Programming With Microsoft Visual Basic 2010 2nd
Edition Diane Zak

https://ebookbell.com/product/clearly-visual-basic-programming-with-
microsoft-visual-basic-2010-2nd-edition-diane-zak-2382420

Microsoft Aspnet Programming With Visual Basic Net Version 2003 Step
By Step 1st Edition G Andrew Duthie

https://ebookbell.com/product/microsoft-aspnet-programming-with-
visual-basic-net-version-2003-step-by-step-1st-edition-g-andrew-
duthie-1352776

Programming With Microsoft Visual C Net Sixth Edition George Shepherd


David Kruglinski

https://ebookbell.com/product/programming-with-microsoft-visual-c-net-
sixth-edition-george-shepherd-david-kruglinski-62157456
PROGRAMMING WITH
MICROSOFT® VISUAL BASIC® 2012

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
SIXTH EDITION

PROGRAMMING
WITH MICROSOFT®
VISUAL BASIC® 2012

DIANE ZAK

Australia l Brazil l Japan l Korea l Mexico l Singapore l Spain l United Kingdom l United States

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
This is an electronic version of the print textbook. Due to electronic rights restrictions, some third party content may be suppressed. Editorial
review has deemed that any suppressed content does not materially affect the overall learning experience. The publisher reserves the right to
remove content from this title at any time if subsequent rights restrictions require it. For valuable information on pricing, previous
editions, changes to current editions, and alternate formats, please visit www.cengage.com/highered to search by
ISBN#, author, title, or keyword for materials in your areas of interest.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Programming with Microsoft® © 2014 Course Technology, Cengage Learning
Visual Basic® 2012, Sixth Edition
ALL RIGHTS RESERVED. No part of this work covered by the copyright herein may be
Diane Zak reproduced, transmitted, stored, or used in any form or by any means graphic, electronic,
or mechanical, including but not limited to photocopying, recording, scanning, digitizing,
Executive Editor: Kathleen McMahon
taping, Web distribution, information networks, or information storage and retrieval
Senior Product Manager: Alyssa Pratt systems, except as permitted under Section 107 or 108 of the 1976 United States
Editorial Assistant: Sarah Ryan Copyright Act, without the prior written permission of the publisher.
Brand Marketing Manager: Kay Stefanski
Senior Content Project Manager: For product information and technology assistance, contact us at
Matthew Hutchinson Cengage Learning Customer & Sales Support, 1-800-354-9706
Quality Assurance: Nicole Spoto
For permission to use material from this text or product,
Art Director: Cheryl Pearl, GEX submit all requests online at www.cengage.com/permissions.
Cover Designer: Cheryl Pearl, GEX Further permissions questions can be emailed to
permissionrequest@cengage.com.
Print Buyer: Julio Esperas
Proofreader: Kathy Orrino
Indexer: Alexandra Nickerson Library of Congress Control Number: 2012956117
Compositor: ISBN-13: 978-1-285-07792-5
Integra Software Services Pvt. Ltd.
ISBN-10: 1-285-07792-X

Course Technology
20 Channel Center Street
Boston, MA 02210
USA

Cengage Learning is a leading provider of customized learning solutions with office


locations around the globe, including Singapore, the United Kingdom, Australia,
Mexico, Brazil and Japan. Locate your local office at international.cengage.com/region
Cengage Learning products are represented in Canada by Nelson Education, Ltd.
For your course and learning solutions, visit www.cengage.com.
Purchase any of our products at your local college store or at our preferred online store
www.cengagebrain.com.
Instructors: Please visit login.cengage.com and log in to access instructor-specific
resources.

Printed in the United States of America


1 2 3 4 5 6 18 17 16 15 14 13

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Brief Contents
v

Preface . . . . . . . . . . . . . . . . . . . . . . . . xviii
Read This Before You Begin . . . . . . . . . . . . . . . . xxii

OVERVIEW An Introduction to Programming . . . . . . . . . . . . . . . 1

C H AP T E R 1 An Introduction to Visual Basic 2012 . . . . . . . . . . . . . 9

C H AP T E R 2 Designing Applications . . . . . . . . . . . . . . . . . . 59

C H AP T E R 3 Using Variables and Constants . . . . . . . . . . . . . . . 113

C H AP T E R 4 The Selection Structure . . . . . . . . . . . . . . . . . . 183

C H AP T E R 5 More on the Selection Structure . . . . . . . . . . . . . . 255

C H AP T E R 6 The Repetition Structure . . . . . . . . . . . . . . . . . 329

C H AP T E R 7 Sub and Function Procedures . . . . . . . . . . . . . . . 409

C H AP T E R 8 String Manipulation . . . . . . . . . . . . . . . . . . . . 469

C H AP T E R 9 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . 525

C H AP T E R 1 0 Structures and Sequential Access Files . . . . . . . . . . . 591

C H AP T E R 1 1 Classes and Objects . . . . . . . . . . . . . . . . . . . 643

C H AP T E R 1 2 Web Applications . . . . . . . . . . . . . . . . . . . . . 707

C H AP T E R 1 3 Working with Access Databases and LINQ . . . . . . . . . . 763

C H AP T E R 1 4 Access Databases and SQL . . . . . . . . . . . . . . . . 821

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
BRIEF CONTENTS

A PP E N D I X A Finding and Fixing Program Errors . . . . . . . . . . . . . 877

A PP E N D I X B GUI Design Guidelines . . . . . . . . . . . . . . . . . . 895

A PP E N D I X C Visual Basic Conversion Functions . . . . . . . . . . . . . 899


vi
A PP E N D I X D Visual Basic 2012 Cheat Sheet . . . . . . . . . . . . . . 901

A PP E N D I X E Case Projects . . . . . . . . . . . . . . . . . . . . . . 917

A PP E N D I X F Applications with Multiple Forms . . . . . . . . . . . Online

Index . . . . . . . . . . . . . . . . . . . . . . . . . . 921

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Contents
vii

Preface . . . . . . . . . . . . . . . . . . . . . . . xviii
Read This Before You Begin . . . . . . . . . . . . . . xxii

OVERVIEW An Introduction to Programming . . . . . . . . . . . . . . 1


Programming a Computer . . . . . . . . . . . . . . . . . . . . . . . . . . 2
The Programmer’s Job . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Employment Opportunities . . . . . . . . . . . . . . . . . . . . . . . . . 2
Visual Basic 2012 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
A Visual Basic 2012 Demonstration . . . . . . . . . . . . . . . . . . . . . 4
Using the Chapters Effectively . . . . . . . . . . . . . . . . . . . . . . . . 5
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

C H AP T E R 1 An Introduction to Visual Basic 2012 . . . . . . . . . . . . 9


LESSON A T h e Sp l as h S c r e en A p p l ic at i o n . . . . . . . . . . . . . . . . . . . 11
Managing the Windows in the IDE . . . . . . . . . . . . . . . . . . . . . . 15
The Windows Form Designer Window . . . . . . . . . . . . . . . . . . . . 16
The Solution Explorer Window . . . . . . . . . . . . . . . . . . . . . . . 17
The Properties Window . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Properties of a Windows Form . . . . . . . . . . . . . . . . . . . . . . . . 20
The Name Property . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
The Text Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
The StartPosition Property . . . . . . . . . . . . . . . . . . . . . . . . . 21
The Font Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
The Size Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Setting and Restoring a Property’s Value . . . . . . . . . . . . . . . . . . . . 22
Saving a Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Closing the Current Solution . . . . . . . . . . . . . . . . . . . . . . . . . 24
Opening an Existing Solution . . . . . . . . . . . . . . . . . . . . . . . . . 24
Exiting Visual Studio 2012 . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Lesson A Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Lesson A Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Lesson A Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 27
Lesson A Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

LESSON B T h e T o o lb o x W i n d o w . . . . . . . . . . . . . . . . . . . . . . . . 29
The Label Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Setting the Text Property . . . . . . . . . . . . . . . . . . . . . . . . . 32
Setting the Location Property . . . . . . . . . . . . . . . . . . . . . . . 32

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CONTENTS

Changing a Property For Multiple Controls . . . . . . . . . . . . . . . . . . . 33


Using the Format Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
The PictureBox Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
The Button Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Starting and Ending an Application . . . . . . . . . . . . . . . . . . . . . . 36
The Code Editor Window . . . . . . . . . . . . . . . . . . . . . . . . . . 38
viii The Me.Close() Instruction . . . . . . . . . . . . . . . . . . . . . . . . . 40
Lesson B Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Lesson B Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Lesson B Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 44
Lesson B Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

LESSON C Using the Timer Tool . . . . . . . . . . . . . . . . . . . . . . . . 48


Setting the FormBorderStyle Property . . . . . . . . . . . . . . . . . . . . . 50
The MinimizeBox, MaximizeBox, and ControlBox Properties . . . . . . . . . . . . 50
Printing the Application’s Code and Interface . . . . . . . . . . . . . . . . . . 51
Lesson C Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Lesson C Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Lesson C Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 53
Lesson C Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

C H AP T E R 2 Designing Applications . . . . . . . . . . . . . . . . . 59
LESSON A C r ea t in g a n O b j ec t- O r i en t ed Ap p li ca t io n . . . . . . . . . . . . . . . 62
Planning an Object-Oriented Application . . . . . . . . . . . . . . . . . . . . 62
Identifying the Application’s Tasks . . . . . . . . . . . . . . . . . . . . . 63
Identifying the Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Identifying the Events . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Drawing a Sketch of the User Interface . . . . . . . . . . . . . . . . . . . 67
Lesson A Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Lesson A Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Lesson A Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 70
Lesson A Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

LESSON B B u il din g t h e U s e r In t er f ac e . . . . . . . . . . . . . . . . . . . . . 72
Including Graphics in the User Interface . . . . . . . . . . . . . . . . . . . 73
Selecting Fonts for the Interface . . . . . . . . . . . . . . . . . . . . . . 73
Adding Color to the Interface . . . . . . . . . . . . . . . . . . . . . . . 74
The BorderStyle, AutoSize, and TextAlign Properties . . . . . . . . . . . . . . 74
Adding a Text Box Control to the Form . . . . . . . . . . . . . . . . . . . 76
Locking the Controls on a Form . . . . . . . . . . . . . . . . . . . . . . . 77
Assigning Access Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Controlling the Tab Order . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Lesson B Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Lesson B Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Lesson B Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 82
Lesson B Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

LESSON C C o d i ng t h e Ap p li ca t io n . . . . . . . . . . . . . . . . . . . . . . . 86
Using Pseudocode to Plan a Procedure . . . . . . . . . . . . . . . . . . . 87
Using a Flowchart to Plan a Procedure . . . . . . . . . . . . . . . . . . . 88
Coding the btnClear Control’s Click Event Procedure . . . . . . . . . . . . . . . 89
Assigning a Value to a Property during Run Time . . . . . . . . . . . . . . . 89

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Using the Focus Method . . . . . . . . . . . . . . . . . . . . . . . . . 91
Internally Documenting the Program Code . . . . . . . . . . . . . . . . . . 91
Coding the btnPrint Control’s Click Event Procedure . . . . . . . . . . . . . . . 93
Writing Arithmetic Expressions . . . . . . . . . . . . . . . . . . . . . . . . 95
Coding the btnCalc Control’s Click Event Procedure . . . . . . . . . . . . . . . 97
The Val Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
The Format Function . . . . . . . . . . . . . . . . . . . . . . . . . . 100 ix
Testing and Debugging the Application . . . . . . . . . . . . . . . . . . . 101
Assembling the Documentation . . . . . . . . . . . . . . . . . . . . . . . 104
Lesson C Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Lesson C Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Lesson C Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 107
Lesson C Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

C H AP T E R 3 Using Variables and Constants . . . . . . . . . . . . . . 113


LESSON A U s in g Va r i ab l es t o S t o r e I n f o r m a ti o n . . . . . . . . . . . . . . . . 116
Selecting a Data Type for a Variable . . . . . . . . . . . . . . . . . . . 116
Selecting a Name for a Variable . . . . . . . . . . . . . . . . . . . . . 118
Declaring a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Assigning Data to an Existing Variable . . . . . . . . . . . . . . . . . . . . 120
The TryParse Method . . . . . . . . . . . . . . . . . . . . . . . . . . 121
The Convert Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
The Scope and Lifetime of a Variable . . . . . . . . . . . . . . . . . . . . 124
Variables with Procedure Scope . . . . . . . . . . . . . . . . . . . . . 125
Variables with Class Scope . . . . . . . . . . . . . . . . . . . . . . . 128
Static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Named Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Option Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Option Explicit and Option Infer . . . . . . . . . . . . . . . . . . . . . . 135
Option Strict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Lesson A Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Lesson A Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Lesson A Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 140
Lesson A Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

LESSON B M o d if yin g t h e P l ay It A g ai n M o v ies Ap p li ca t io n . . . . . . . . . . . 146


Modifying the Calculate Button’s Code . . . . . . . . . . . . . . . . . . . . 147
Concatenating Strings . . . . . . . . . . . . . . . . . . . . . . . . . . 154
The InputBox Function . . . . . . . . . . . . . . . . . . . . . . . . . . 155
The ControlChars.Newline Constant . . . . . . . . . . . . . . . . . . . . . 159
Designating a Default Button . . . . . . . . . . . . . . . . . . . . . . . . 160
Using the ToString Method to Format Numbers . . . . . . . . . . . . . . . . 161
Lesson B Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Lesson B Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Lesson B Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 165
Lesson B Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

LESSON C M o di f yi n g th e Lo a d an d C l ic k E ve n t Pr o ce d u r es . . . . . . . . . . . 1 7 1
Coding the TextChanged Event Procedure . . . . . . . . . . . . . . . . . . 174
Associating a Procedure with Different Objects and Events . . . . . . . . . . 174

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CONTENTS

Lesson C Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 178


Lesson C Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Lesson C Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 179
Lesson C Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 180

C H AP T E R 4 The Selection Structure . . . . . . . . . . . . . . . . . 183


x
LESSON A M a k in g D ec is io n s i n a Pr o gr am . . . . . . . . . . . . . . . . . . 186
Flowcharting a Selection Structure . . . . . . . . . . . . . . . . . . . . . 189
Coding Selection Structures in Visual Basic . . . . . . . . . . . . . . . . . . 192
Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Using Comparison Operators: Swapping Numeric Values . . . . . . . . . . . 197
Using Comparison Operators: Displaying the Sum or Difference . . . . . . . . 200
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Using the Truth Tables . . . . . . . . . . . . . . . . . . . . . . . . . 206
Using Logical Operators: Calculating Gross Pay . . . . . . . . . . . . . . . 207
Comparing Strings Containing Letters . . . . . . . . . . . . . . . . . . . . 209
Converting a String to Uppercase or Lowercase . . . . . . . . . . . . . . . 211
Using the ToUpper and ToLower Methods: Displaying a Message . . . . . . . 212
Summary of Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Lesson A Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Lesson A Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Lesson A Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 218
Lesson A Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

LESSON B Cr ea ti n g th e C o vi n gt o n R es o r t A p p li ca t io n . . . . . . . . . . . . . 223
Adding a Group Box to the Form . . . . . . . . . . . . . . . . . . . . . 224
Coding the Covington Resort Application . . . . . . . . . . . . . . . . . . . 226
Coding the btnCalc Control’s Click Event Procedure . . . . . . . . . . . . . 226
The MessageBox.Show Method . . . . . . . . . . . . . . . . . . . . . . 230
Completing the btnCalc_Click Procedure . . . . . . . . . . . . . . . . . . . 233
Lesson B Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Lesson B Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Lesson B Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 237
Lesson B Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

LESSON C C o d i ng t h e Ke yP r es s E v en t P r o c ed u r e s . . . . . . . . . . . . . . 240
Coding the Enter Event Procedures . . . . . . . . . . . . . . . . . . . . . 243
Lesson C Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Lesson C Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Lesson C Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 247
Lesson C Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 248

C H AP T E R 5 More on the Selection Structure . . . . . . . . . . . . . 255


LESSON A N e s te d S el ec ti o n S t r u ct u r e s . . . . . . . . . . . . . . . . . . . 258
Flowcharting a Nested Selection Structure . . . . . . . . . . . . . . . . . . 261
Coding a Nested Selection Structure . . . . . . . . . . . . . . . . . . . . 263
Logic Errors in Selection Structures . . . . . . . . . . . . . . . . . . . . . 266
First Logic Error: Using a Compound Condition Rather
Than a Nested Selection Structure . . . . . . . . . . . . . . . . . . . 268
Second Logic Error: Reversing the Outer and Nested Decisions . . . . . . . . 269

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Third Logic Error: Using an Unnecessary Nested Selection Structure . . . . . . 270
Fourth Logic Error: Including an Unnecessary Comparison in a Condition . . . . 271
Multiple-Alternative Selection Structures . . . . . . . . . . . . . . . . . . . 272
The Select Case Statement . . . . . . . . . . . . . . . . . . . . . . . . 275
Specifying a Range of Values in a Case Clause . . . . . . . . . . . . . . . 277
Lesson A Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Lesson A Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 xi
Lesson A Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 280
Lesson A Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

LESSON B M o d if yin g t h e C o v in g t o n Re s o r t A p p l ic at i o n . . . . . . . . . . . . . 288


Adding a Radio Button to the Interface . . . . . . . . . . . . . . . . . . . 290
Adding a Check Box to the Interface . . . . . . . . . . . . . . . . . . . 291
Modifying the Calculate Button’s Code . . . . . . . . . . . . . . . . . . . . 293
Comparing Boolean Values . . . . . . . . . . . . . . . . . . . . . . . 297
Modifying the ClearLabels Procedure . . . . . . . . . . . . . . . . . . . . 300
Lesson B Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Lesson B Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Lesson B Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 305
Lesson B Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 307

LESSON C U s in g t h e T r yP ar s e M et h o d f o r D at a V a li d a ti o n . . . . . . . . . . . 312
Generating Random Integers . . . . . . . . . . . . . . . . . . . . . . . . 315
Showing and Hiding a Control . . . . . . . . . . . . . . . . . . . . . . . 319
Lesson C Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Lesson C Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Lesson C Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 322
Lesson C Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

C H AP T E R 6 The Repetition Structure . . . . . . . . . . . . . . . . 329


LESSON A R epe at i ng P r o g r a m In s t r u ct i o n s . . . . . . . . . . . . . . . . . . 331
The Savings Account Application . . . . . . . . . . . . . . . . . . . . . 334
The Do…Loop Statement . . . . . . . . . . . . . . . . . . . . . . . . . 337
Coding the Modified Savings Account Application . . . . . . . . . . . . . . 340
Counters and Accumulators . . . . . . . . . . . . . . . . . . . . . . . . 343
The Addition Application . . . . . . . . . . . . . . . . . . . . . . . . . 345
The Sales Express Application . . . . . . . . . . . . . . . . . . . . . . 349
Arithmetic Assignment Operators . . . . . . . . . . . . . . . . . . . . . . 355
The For…Next Statement . . . . . . . . . . . . . . . . . . . . . . . . . 357
A Different Version of the Savings Account Application . . . . . . . . . . . . 360
Comparing the For…Next and Do…Loop Statements . . . . . . . . . . . . 363
Lesson A Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Lesson A Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Lesson A Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 366
Lesson A Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

LESSON B Nes t e d R epe t it io n S tr u c t ur es . . . . . . . . . . . . . . . . . . . 375


The Refresh and Sleep Methods . . . . . . . . . . . . . . . . . . . . . . 377
Trixie at the Diner . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Revisiting the Savings Account Application . . . . . . . . . . . . . . . . . . 379
A Caution about Real Numbers . . . . . . . . . . . . . . . . . . . . . . 382

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CONTENTS

Lesson B Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 385


Lesson B Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Lesson B Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 385
Lesson B Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 386

LESSON C C r ea t in g t he G r o s s P ay A p p l ic at i o n . . . . . . . . . . . . . . . . 388
xii Including a List Box in an Interface . . . . . . . . . . . . . . . . . . . . . 388
Adding Items to a List Box . . . . . . . . . . . . . . . . . . . . . . . 389
The Sorted Property . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Coding the Gross Pay Application . . . . . . . . . . . . . . . . . . . . . . 391
The SelectedItem and SelectedIndex Properties . . . . . . . . . . . . . . . 393
The SelectedValueChanged and SelectedIndexChanged Events . . . . . . . . 395
Coding the Calculate Button’s Click Event Procedure . . . . . . . . . . . . . 396
Lesson C Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Lesson C Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Lesson C Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 399
Lesson C Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 400

C H AP T E R 7 Sub and Function Procedures . . . . . . . . . . . . . . 409


LESSON A S u b Pr o ce du r es . . . . . . . . . . . . . . . . . . . . . . . . . 4 12
Passing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Passing Variables by Value . . . . . . . . . . . . . . . . . . . . . . . 415
Passing Variables by Reference . . . . . . . . . . . . . . . . . . . . . 418
Function Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Lesson A Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Lesson A Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Lesson A Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 431
Lesson A Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 434

LESSON B In cl u din g a C o m b o B o x in a n In t er f a ce . . . . . . . . . . . . . . . 439


Lesson B Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Lesson B Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Lesson B Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 445
Lesson B Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 445

LESSON C C r ea t in g t he C er r ut i C o m p a ny A p p l ic at i o n . . . . . . . . . . . . . 448
Coding the FormClosing Event Procedure . . . . . . . . . . . . . . . . . . 449
Coding the btnCalc_Click Procedure . . . . . . . . . . . . . . . . . . . . 451
Creating the GetFwt Function . . . . . . . . . . . . . . . . . . . . . . 453
Completing the btnCalc_Click Procedure . . . . . . . . . . . . . . . . . . . 457
Rounding Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Lesson C Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Lesson C Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Lesson C Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 465
Lesson C Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 466

C H AP T E R 8 String Manipulation . . . . . . . . . . . . . . . . . . . 469


LESSON A W o r k in g w it h S t r i ng s . . . . . . . . . . . . . . . . . . . . . . . 473
Determining the Number of Characters in a String . . . . . . . . . . . . . . . 473
Removing Characters from a String . . . . . . . . . . . . . . . . . . . . . 474
The Product ID Application . . . . . . . . . . . . . . . . . . . . . . . 475

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Inserting Characters in a String . . . . . . . . . . . . . . . . . . . . . . 476
Aligning the Characters in a String . . . . . . . . . . . . . . . . . . . . 477
The Net Pay Application . . . . . . . . . . . . . . . . . . . . . . . . . 478
Searching a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
The City and State Application . . . . . . . . . . . . . . . . . . . . . . 481
Accessing the Characters in a String . . . . . . . . . . . . . . . . . . . . 483
The Rearrange Name Application . . . . . . . . . . . . . . . . . . . . . 484 xiii
Using Pattern-Matching to Compare Strings . . . . . . . . . . . . . . . . . 486
Modifying the Product ID Application . . . . . . . . . . . . . . . . . . . 488
Lesson A Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
Lesson A Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Lesson A Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 491
Lesson A Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 494

LESSON B Ad d in g a M en u t o a F o r m . . . . . . . . . . . . . . . . . . . . . 497
Assigning Shortcut Keys to Menu Items . . . . . . . . . . . . . . . . . . 501
Coding the Exit Menu Item . . . . . . . . . . . . . . . . . . . . . . . . 503
Coding the txtLetter Control’s KeyPress Event . . . . . . . . . . . . . . . 503
Lesson B Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Lesson B Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Lesson B Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 504
Lesson B Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 505

LESSON C C o m pl et in g t h e F r a n k en s t ei n Ga m e A p p lic a ti o n . . . . . . . . . . . 506


Coding the FILE Menu’s New Game Option . . . . . . . . . . . . . . . . . 507
Completing the Check Button’s Click Event Procedure . . . . . . . . . . . . 511
Lesson C Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Lesson C Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Lesson C Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 519
Lesson C Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

C H AP T E R 9 Arrays . . . . . . . . . . . . . . . . . . . . . . . . 525
LESSON A A r r a ys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 528
Declaring a One-Dimensional Array . . . . . . . . . . . . . . . . . . . . 528
Storing Data in a One-Dimensional Array . . . . . . . . . . . . . . . . . . 530
Determining the Number of Elements in a One-Dimensional Array . . . . . . . . 531
Determining the Highest Subscript in a One-Dimensional Array . . . . . . . . . 531
Traversing a One-Dimensional Array . . . . . . . . . . . . . . . . . . . . 532
The For Each…Next Statement . . . . . . . . . . . . . . . . . . . . . . 533
Calculating the Total and Average Values . . . . . . . . . . . . . . . . . . 535
Finding the Highest Value . . . . . . . . . . . . . . . . . . . . . . . . . 537
Sorting a One-Dimensional Array . . . . . . . . . . . . . . . . . . . . . . 542
Lesson A Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Lesson A Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Lesson A Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 546
Lesson A Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 549

LESSON B Ar r ay s an d C o ll ec ti o n s . . . . . . . . . . . . . . . . . . . . . . 5 5 4
Accumulator and Counter Arrays . . . . . . . . . . . . . . . . . . . . . . 558
Parallel One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . 561

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CONTENTS

The Die Tracker Application . . . . . . . . . . . . . . . . . . . . . . . . 567


Lesson B Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
Lesson B Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
Lesson B Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 572
Lesson B Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 572

xiv LESSON C T w o - Di m en s io n a l Ar r a y s . . . . . . . . . . . . . . . . . . . . . 576


Traversing a Two-Dimensional Array . . . . . . . . . . . . . . . . . . . . 579
Totaling the Values Stored in a Two-Dimensional Array . . . . . . . . . . . . . 580
Searching a Two-Dimensional Array . . . . . . . . . . . . . . . . . . . . . 582
Lesson C Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
Lesson C Key Term . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
Lesson C Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 586
Lesson C Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 588

C H AP T E R 1 0 Structures and Sequential Access Files . . . . . . . . . . 591


LESSON A S t r u c tu r e s . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
Declaring and Using a Structure Variable . . . . . . . . . . . . . . . . . . . 595
Passing a Structure Variable to a Procedure . . . . . . . . . . . . . . . . . 596
Creating an Array of Structure Variables . . . . . . . . . . . . . . . . . . . 600
Lesson A Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
Lesson A Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
Lesson A Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 606
Lesson A Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 606

LESSON B Seq ue n ti al A c ce s s F ile s . . . . . . . . . . . . . . . . . . . . . 610


Writing Data to a Sequential Access File . . . . . . . . . . . . . . . . . . . 610
Closing an Output Sequential Access File . . . . . . . . . . . . . . . . . . 613
Reading Data from a Sequential Access File . . . . . . . . . . . . . . . . . 614
Closing an Input Sequential Access File . . . . . . . . . . . . . . . . . . . 617
Lesson B Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Lesson B Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Lesson B Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 622
Lesson B Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 623

LESSON C C o d i ng t h e CD C o l lec t io n A p p li ca t io n . . . . . . . . . . . . . . . 626


Coding the Form’s Load Event Procedure . . . . . . . . . . . . . . . . . . 627
Coding the btnAdd_Click Procedure . . . . . . . . . . . . . . . . . . . . . 629
Aligning Columns of Information . . . . . . . . . . . . . . . . . . . . . . 630
Coding the btnRemove_Click Procedure . . . . . . . . . . . . . . . . . . . 633
Coding the Form’s FormClosing Event Procedure . . . . . . . . . . . . . . . 634
Lesson C Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
Lesson C Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
Lesson C Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 638
Lesson C Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 639

C H AP T E R 1 1 Classes and Objects . . . . . . . . . . . . . . . . . . 643


LESSON A O b jec t -O r ien t ed P r o g r am m i n g T er m i no lo g y . . . . . . . . . . . . . 6 4 6
Creating a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
Example 1—A Class that Contains Public Variables Only . . . . . . . . . . . . 648

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Example 2—A Class that Contains Private Variables, Public Properties,
and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
Private Variables and Property Procedures . . . . . . . . . . . . . . . . . 654
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
Methods Other than Constructors . . . . . . . . . . . . . . . . . . . . . 659
Coding the Carpets Galore Application . . . . . . . . . . . . . . . . . . . 660
Example 3—A Class that Contains a Parameterized Constructor . . . . . . . . . 664 xv
Example 4—Reusing a Class . . . . . . . . . . . . . . . . . . . . . . . 668
Lesson A Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 672
Lesson A Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
Lesson A Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 674
Lesson A Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 675

LESSON B Ex a mp le 5 — A C la s s t ha t C o n ta in s a R ea d O nl y P r o p er t y . . . . . . . 680
Example 6—A Class that Contains Auto-Implemented Properties . . . . . . . . . 685
Example 7—A Class that Contains Overloaded Methods . . . . . . . . . . . . 687
Lesson B Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
Lesson B Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
Lesson B Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 694
Lesson B Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 695

LESSON C E x a m p l e 8 —U s i ng a B as e C la ss an d a D er iv ed Cl as s . . . . . . . . 698
Lesson C Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
Lesson C Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
Lesson C Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 704
Lesson C Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 705

C H AP T E R 1 2 Web Applications . . . . . . . . . . . . . . . . . . . . 707


LESSON A W e b A pp lic a ti o n s . . . . . . . . . . . . . . . . . . . . . . . . 710
Creating a Web Application . . . . . . . . . . . . . . . . . . . . . . . . 713
Adding the Default.aspx Web Page to the Application . . . . . . . . . . . . . . 715
Including a Title on a Web Page . . . . . . . . . . . . . . . . . . . . . 717
Adding Static Text to a Web Page . . . . . . . . . . . . . . . . . . . . 717
Viewing a Web Page in Full Screen View . . . . . . . . . . . . . . . . . . . 719
Adding Another Web Page to the Application . . . . . . . . . . . . . . . . . 720
Adding a Link Button Control to a Web Page . . . . . . . . . . . . . . . . . 720
Starting a Web Application . . . . . . . . . . . . . . . . . . . . . . . . 722
Adding an Image to a Web Page . . . . . . . . . . . . . . . . . . . . . . 724
Closing and Opening an Existing Web Application . . . . . . . . . . . . . . . 726
Repositioning a Control on a Web Page . . . . . . . . . . . . . . . . . . . 726
Lesson A Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 728
Lesson A Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
Lesson A Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 729
Lesson A Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 730

LESSON B Dy n am i c W eb Pa g es . . . . . . . . . . . . . . . . . . . . . . . 732
Coding the Submit Button’s Click Event Procedure . . . . . . . . . . . . . . . 735
Validating User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
Lesson B Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
Lesson B Key Term . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
Lesson B Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 742
Lesson B Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 742

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CONTENTS

LESSON C C r ea t in g t he D J T o m A p p li ca t io n . . . . . . . . . . . . . . . . . 746
Creating a Columnar Layout . . . . . . . . . . . . . . . . . . . . . . . . 747
Using an ASP Table . . . . . . . . . . . . . . . . . . . . . . . . . . . 748
Dragging Controls in Source View . . . . . . . . . . . . . . . . . . . . 751
Adding Items to a DropDownList Control . . . . . . . . . . . . . . . . . . . 754
Coding DJ Tom’s Web Page . . . . . . . . . . . . . . . . . . . . . . . . 755
xvi Using the <br /> Tag . . . . . . . . . . . . . . . . . . . . . . . . . 756
Lesson C Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 758
Lesson C Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 758
Lesson C Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 759
Lesson C Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 760

C H AP T E R 1 3 Working with Access Databases and LINQ . . . . . . . . . 763


LESSON A D at a b a se T e r m in o lo gy . . . . . . . . . . . . . . . . . . . . . . 766
Connecting an Application to a Microsoft Access Database . . . . . . . . . . . 768
Previewing the Contents of a Dataset . . . . . . . . . . . . . . . . . . . 771
Binding the Objects in a Dataset . . . . . . . . . . . . . . . . . . . . . . 772
Having the Computer Create a Bound Control . . . . . . . . . . . . . . . . 773
The DataGridView Control . . . . . . . . . . . . . . . . . . . . . . . . 776
Visual Basic Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 781
Handling Errors in the Code . . . . . . . . . . . . . . . . . . . . . . . 781
The Copy to Output Directory Property . . . . . . . . . . . . . . . . . . . 784
Binding to an Existing Control . . . . . . . . . . . . . . . . . . . . . . . 786
Coding the Next Record and Previous Record Buttons . . . . . . . . . . . . . 789
Lesson A Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 790
Lesson A Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 791
Lesson A Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 792
Lesson A Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 794

LESSON B Cr ea ti n g a Q ue r y . . . . . . . . . . . . . . . . . . . . . . . . 796
Customizing a BindingNavigator Control . . . . . . . . . . . . . . . . . . . 800
Using the LINQ Aggregate Operators . . . . . . . . . . . . . . . . . . . . 802
Lesson B Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 806
Lesson B Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 807
Lesson B Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 807
Lesson B Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 809

LESSON C C o m p l et in g t h e P a r ad is e B o o k s t o r e A p p l ic at io n . . . . . . . . . . . 811
Coding the Paradise Bookstore Application . . . . . . . . . . . . . . . . . . 813
Lesson C Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 815
Lesson C Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 815
Lesson C Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 816
Lesson C Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 817

C H AP T E R 1 4 Access Databases and SQL . . . . . . . . . . . . . . . 821


LESSON A A dd i n g Re co r d s t o a Da t as e t . . . . . . . . . . . . . . . . . . . 824
Sorting the Records in a Dataset . . . . . . . . . . . . . . . . . . . . . . 831
Deleting Records from a Dataset . . . . . . . . . . . . . . . . . . . . . . 833
Lesson A Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 837

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Lesson A Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 837
Lesson A Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 838
Lesson A Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 838

LESSON B S t r u ct u r ed Q u er y L an g u ag e . . . . . . . . . . . . . . . . . . . . 841
The SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 841
Creating a Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843 xvii
Lesson B Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 850
Lesson B Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
Lesson B Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 851
Lesson B Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 853

LESSON C P ar am e te r Q u e r ie s . . . . . . . . . . . . . . . . . . . . . . . . 855
Saving a Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
Invoking a Query from Code . . . . . . . . . . . . . . . . . . . . . . . . 861
The INSERT and DELETE Statements . . . . . . . . . . . . . . . . . . . . 863
Lesson C Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 873
Lesson C Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . 873
Lesson C Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 873
Lesson C Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 874

A PP E N D I X A Finding and Fixing Program Errors . . . . . . . . . . . 877

A PP E N D I X B GUI Design Guidelines . . . . . . . . . . . . . . . . . 895

A PP E N D I X C Visual Basic Conversion Functions . . . . . . . . . . . 899

A PP E N D I X D Visual Basic 2012 Cheat Sheet . . . . . . . . . . . . 901

A PP E N D I X E Case Projects . . . . . . . . . . . . . . . . . . . . 917

A PP E N D I X F Applications with Multiple Forms . . . . . . . . . . . Online

Index . . . . . . . . . . . . . . . . . . . . . . . . 921

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Preface
xviii

Programming with Microsoft Visual Basic 2012, Sixth Edition uses Visual Basic 2012, an object-oriented
language, to teach programming concepts. This book is designed for a beginning programming course.
However, it assumes students are familiar with basic Windows skills and file management.

Organization and Coverage


Programming with Microsoft Visual Basic 2012, Sixth Edition contains an Overview and 14 chapters that present
hands-on instruction; it also contains five appendices (A through E). An additional appendix (Appendix F)
covering multiple-form applications and the FontDialog, ColorDialog, and TabControl tools is available online
at www.cengagebrain.com.
In the chapters, students with no previous programming experience learn how to plan and create their own
interactive Windows applications. GUI design skills, OOP concepts, and planning tools (such as TOE charts,
pseudocode, and flowcharts) are emphasized throughout the book. The chapters show students how to work
with objects and write Visual Basic statements such as If…Then…Else, Select Case, Do…Loop, For…Next,
and For Each…Next. Students also learn how to create and manipulate variables, constants, strings, sequential
access files, structures, classes, and arrays. Chapter 12 shows students how to create both static and dynamic
Web applications. In Chapter 13, students learn how to connect an application to a Microsoft Access database,
and then use Language Integrated Query (LINQ) to query the database. Chapter 14 continues the coverage
of databases, introducing the student to more advanced concepts and Structured Query Language (SQL).
Appendix A, which can be covered after Chapter 3, teaches students how to locate and correct errors in their
code. The appendix shows students how to step through their code and also how to create breakpoints.
Appendix B recaps the GUI design guidelines mentioned in the chapters, and Appendix C lists the Visual Basic
conversion functions. The Visual Basic 2012 Cheat Sheet contained in Appendix D summarizes important
concepts covered in the chapters, such as the syntax of statements, methods, and so on. The Cheat Sheet
provides a convenient place for students to locate the information they need as they are creating and coding
their applications. Appendix E contains Case Projects that can be assigned after completing specific chapters in
the book.

Approach
Programming with Microsoft Visual Basic 2012, Sixth Edition teaches programming concepts using a task-driven
rather than a command-driven approach. By working through the chapters, which are each motivated by a
realistic case, students learn how to develop applications they are likely to encounter in the workplace. This is
much more effective than memorizing a list of commands out of context. The book motivates students by
demonstrating why they need to learn the concepts and skills covered in each chapter.

Features
Programming with Microsoft Visual Basic 2012, Sixth Edition is an exceptional textbook because it also includes
the following features:

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Organization and Coverage

READ THIS BEFORE YOU BEGIN This section is consistent with Course Technology’s unequaled commitment to
helping instructors introduce technology into the classroom. Technical considerations and assumptions about
hardware, software, and default settings are listed in one place to help instructors save time and eliminate
unnecessary aggravation.
YOU DO IT! BOXES These boxes provide simple applications that allow students to demonstrate their
understanding of a concept before moving on to the next concept. The YOU DO IT! boxes are located almost xix
exclusively in Lesson A of each chapter.
VISUAL STUDIO 2012 METHODS The book focuses on Visual Studio 2012 methods rather than on Visual Basic
functions. This is because the Visual Studio methods can be used in any .NET language, whereas the Visual
Basic functions can be used only in Visual Basic. Exceptions to this are the Val and Format functions, which are
introduced in Chapter 2. These functions are covered in the book simply because it is likely that students will
encounter them in existing Visual Basic programs. However, in Chapter 3, the student is taught to use the
TryParse method and the Convert class methods rather than the Val function. Also in Chapter 3, the Format
function is replaced with the ToString method.
OPTION STATEMENTS All programs include the Option Explicit, Option Strict, and Option Infer statements.

START HERE ARROWS These arrows indicate the beginning of a tutorial steps section in the book.

DATABASES, LINQ, AND SQL The book includes two chapters (Chapters 13 and 14) on databases. LINQ is
covered in Chapter 13. SQL is covered in Chapter 14.
FIGURES Figures that introduce new statements, functions, or methods contain both the syntax and examples
of using the syntax. Including the syntax in the figures makes the examples more meaningful, and vice versa.
CHAPTER CASES Each chapter begins with a programming-related problem that students could reasonably
expect to encounter in business, followed by a demonstration of an application that could be used to solve
the problem. Showing the students the completed application before they learn how to create it is motivational
and instructionally sound. By allowing the students to see the type of application they will be able to create
after completing the chapter, the students will be more motivated to learn because they can see how the
programming concepts they are about to learn can be used and, therefore, why the concepts are important.
LESSONS Each chapter is divided into three lessons—A, B, and C. Lesson A introduces the programming
concepts that will be used in the completed application. The concepts are illustrated with code examples and
sample applications. The user interface for each sample application is provided to the student. Also provided are
tutorial-style steps that guide the student on coding, running, and testing the application. Each sample
application allows the student to observe how the current concept can be used before the next concept is
introduced. In Lessons B and/or C, the student creates the application required to solve the problem specified in
the Chapter Case.
APPENDICES Appendix A, which can be covered after Chapter 3, teaches students how to locate and correct
errors (syntax, logic, and run time) in their code. The appendix shows students how to step through their code
and also how to create breakpoints. Appendix B summarizes the GUI design guidelines taught in the chapters,
making it easier for the student to follow the guidelines when designing an application’s interface. Appendix C
lists the Visual Basic conversion functions. Appendix D contains a Cheat Sheet that summarizes important
concepts covered in the chapters, such as the syntax of statements, methods, and so on. The Cheat Sheet
provides a convenient place for students to locate the information they need as they are creating and coding
their applications. Appendix E contains Case Projects that can be assigned after completing specific chapters in
the book. Appendix F, which is available online at www.cengagebrain.com, covers multiple-form applications and
the FontDialog, ColorDialog, and TabControl tools.
GUI DESIGN TIP BOXES The GUI DESIGN TIP boxes contain guidelines and recommendations for designing
applications that follow Windows standards. Appendix B provides a summary of the GUI design guidelines
covered in the chapters.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
PREFACE Instructor Resources

TIP These notes provide additional information about the current concept. Examples include
alternative ways of writing statements or performing tasks, as well as warnings about common
mistakes made when using a particular command and reminders of related concepts learned in
previous chapters.
SUMMARY Each lesson contains a Summary section that recaps the concepts covered in the lesson.
xx KEY TERMS Following the Summary section in each lesson is a listing of the key terms introduced throughout
the lesson, along with their definitions.
REVIEW QUESTIONS Each lesson contains Review Questions designed to test a student’s understanding of the
lesson’s concepts.
EXERCISES The Review Questions in each lesson are followed by Exercises, which provide students with
additional practice of the skills and concepts they learned in the lesson. The Exercises are designated as
INTRODUCTORY, INTERMEDIATE, ADVANCED, DISCOVERY, and SWAT THE BUGS. The DISCOVERY
Exercises encourage students to challenge and independently develop their own programming skills while
exploring the capabilities of Visual Basic 2012. The SWAT THE BUGS Exercises provide an opportunity for
students to detect and correct errors in an application’s code.

New to This Edition!


VIDEOS These notes direct students to videos that accompany each chapter in the book. The videos
explain and/or demonstrate one or more of the chapter’s concepts. The videos have been revised
from the previous edition and are available via the optional CourseMate for this text.
NEW CHAPTER CASES, EXAMPLES, APPLICATIONS, REVIEW QUESTIONS, AND EXERCISES The chapters
contain new Chapter Cases, code examples, sample applications, Review Questions, and Exercises.
APPENDIX D (VISUAL BASIC 2012 CHEAT SHEET) This appendix summarizes important concepts covered in
the chapters (such as the syntax of statements, methods, and so on) and provides a quick reference for students.
APPENDIX E (CASE PROJECTS) This appendix contains Case Projects that can be assigned after completing
specific chapters in the book.

Chapters 4, 5, and 7
The following two topics were moved from Chapter 4 to Chapter 5: the TryParse method’s return value and
the comparison of Boolean values. The Financial.Pmt method was removed from Chapter 4. In the previous
edition of the book, independent Sub procedures were introduced in Chapter 5 and then covered more fully
in Chapter 7. In this edition, independent Sub procedures are now covered in one place: Chapter 7.

Steps and Figures


The tutorial-style steps in the book assume you are using Microsoft Visual Studio Professional 2012 and a
system running either Microsoft Windows 8 or Microsoft Windows 7. The figures in the book reflect how your
screen will look if you are using a Microsoft Windows 8 system. Your screen may appear slightly different in
some instances if you are using a Microsoft Windows 7 system. Any major differences between the screens for
both versions of Microsoft Windows are indicated in the figures.

Instructor Resources
The following teaching tools are available for download at our Instructor Companion Site. Simply search for this
text at login.cengage.com. An instructor login is required.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Acknowledgments

ELECTRONIC INSTRUCTOR’S MANUAL The Instructor’s Manual that accompanies this textbook includes
additional instructional material to assist in class preparation, including items such as Sample Syllabi, Chapter
Outlines, Technical Notes, Lecture Notes, Quick Quizzes, Teaching Tips, Discussion Topics, and Additional
Case Projects.
EXAMVIEW® This textbook is accompanied by ExamView, a powerful testing software package that allows
instructors to create and administer printed, computer (LAN-based), and Internet exams. ExamView includes xxi
hundreds of questions that correspond to the topics covered in this text, enabling students to generate detailed
study guides that include page references for further review. The computer-based and Internet testing
components allow students to take exams at their computers, and also save the instructor time by grading each
exam automatically.
POWERPOINT PRESENTATIONS This book offers Microsoft PowerPoint slides for each chapter. These are
included as a teaching aid for classroom presentation, to make available to students on the network for chapter
review, or to be printed for classroom distribution. Instructors can add their own slides for additional topics
they introduce to the class.
SOLUTION FILES Solutions to the Lesson applications and the end-of-lesson Review Questions and Exercises
are provided.
DATA FILES Data Files are necessary for completing the computer activities in this book. Data Files can also be
downloaded by students at www.cengagebrain.com.

CourseMate
The more you study, the better the results. Make the most of your study time by accessing everything
you need to succeed in one place. Read your textbook, take notes, review flashcards, watch videos, and
take practice quizzes online. CourseMate goes beyond the book to deliver what you need! Learn more at
www.cengage.com/coursemate.
The Visual Basic CourseMate includes:
l Video Lessons: Each chapter is accompanied by several video lessons that help to explain important chapter
concepts. These videos were created and narrated by the author.
l An interactive eBook, quizzes, flashcards, and more!

Instructors may add CourseMate to the textbook package, or students may purchase CourseMate
directly at www.cengagebrain.com.

Acknowledgments
Writing a book is a team effort rather than an individual one. I would like to take this opportunity to thank my
team, especially Alyssa Pratt (Senior Project Manager), Sreejith Govindan (Full Service Project Manager), Nicole
Spoto (Quality Assurance), Matt Hutchinson (Content Project Manager), Kathy Orrino (Proofreader), and
the compositors at Integra. Thank you for your support, enthusiasm, patience, and hard work. Last, but
certainly not least, I want to thank the following reviewers for their invaluable ideas and comments: Mary Brock:
Mississippi University for Women; John Buerck: Saint Louis University; Jane Hammer: Valley City University;
Donna Petty: Wallace Community College; and Helen Schneider: The University of Findlay. And a special thank
you to Sally Douglas (College of Central Florida) for suggesting the YOU DO IT! boxes.
Diane Zak

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Read This Before
You Begin
xxii

Technical Information
Data Files
You will need data files to complete the computer activities in this book. Your instructor may provide the data
files to you. You may obtain the files electronically at www.cengagebrain.com, and then navigating to the page for
this book.
Each chapter in this book has its own set of data files, which are stored in a separate folder within the VB2012
folder. The files for Chapter 1 are stored in the VB2012\Chap01 folder. Similarly, the files for Chapter 2 are
stored in the VB2012\Chap02 folder. Throughout this book, you will be instructed to open files from or save
files to these folders.
You can use a computer in your school lab or your own computer to complete the steps and Exercises
in this book.

Using Your Own Computer


To use your own computer to complete the computer activities in this book, you will need the following:
l A Pentium® 4 processor, 1.6 GHz or higher, personal computer running Microsoft Windows. This book was
written using Microsoft Windows 8, and Quality Assurance tested using Microsoft Windows 7.
l Either Microsoft Visual Studio 2012 or the Express Editions of Microsoft Visual Studio 2012 (namely,
Microsoft Visual Studio Express 2012 for Windows Desktop and Microsoft Visual Studio Express 2012 for
Web) installed on your computer. This book was written and Quality Assurance tested using Microsoft
Visual Studio Professional 2012 and Microsoft Visual Studio Express 2012 for Web. At the time of this
writing, you can download a free copy of the Express Editions at www.microsoft.com/visualstudio/eng/
products/visual-studio-express-products.

Figures
The figures in this book reflect how your screen will look if you are using Microsoft Visual Studio Professional
2012 and a Microsoft Windows 8 system. Your screen may appear slightly different in some instances if you are
using another version of either Microsoft Visual Studio or Microsoft Windows.

Visit Our Web Site


Additional materials designed for this textbook might be available at www.cengagebrain.com. Search this site for
more details.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Technical Information

To the Instructor
To complete the computer activities in this book, your students must use a set of data files. These files can be
obtained electronically at www.cengagebrain.com.
The material in this book was written using Microsoft Visual Studio Professional 2012 on a Microsoft Windows
8 system. It was Quality Assurance tested using Microsoft Visual Studio Professional 2012 on a Microsoft
Windows 7 system, and using Microsoft Visual Studio Express 2012 for Web on a Microsoft Windows 8 system. xxiii

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
OVERVIEW

An Introduction to
Programming

After studying the Overview, you should be able to:

Define the terminology used in programming


Explain the tasks performed by a programmer
Understand the employment opportunities for programmers and
software engineers
Run a Visual Basic 2012 application
Understand how to use the chapters effectively

All Microsoft screenshots used with permission from Microsoft Corporation.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
OVERVIEW An Introduction to Programming

Programming a Computer
In essence, the word programming means giving a mechanism the directions to accomplish a
task. If you are like most people, you’ve already programmed several mechanisms, such as your
digital video recorder (DVR), cell phone, or coffee maker. Like these devices, a computer also is
a mechanism that can be programmed.
2 The directions given to a computer are called computer programs or, more simply, programs.
The people who write programs are called programmers. Programmers use a variety of special
languages, called programming languages, to communicate with the computer. Some popular
programming languages are Visual Basic, C#, C++, and Java. In this book, you will use the Visual
Basic programming language.

The Programmer’s Job


When a company has a problem that requires a computer solution, typically it is a programmer
who comes to the rescue. The programmer might be an employee of the company; or he or she
might be a freelance programmer, which is a programmer who works on temporary contracts
rather than for a long-term employer.
First the programmer meets with the user, which is the person (or persons) responsible for
describing the problem. In many cases, this person or persons also will eventually use the
solution. Depending on the complexity of the problem, multiple programmers may be involved,
and they may need to meet with the user several times. Programming teams often contain
subject matter experts, who may or may not be programmers. For example, an accountant
Overview- might be part of a team working on a program that requires accounting expertise. The purpose
Programmers of the initial meetings with the user is to determine the exact problem and to agree on a
video solution.
After the programmer and user agree on the solution, the programmer begins converting the
solution into a computer program. During the conversion phase, the programmer meets
periodically with the user to determine whether the program fulfills the user’s needs and to
refine any details of the solution. When the user is satisfied that the program does what he or
she wants it to do, the programmer rigorously tests the program with sample data before
releasing it to the user. In many cases, the programmer also provides the user with a manual
that explains how to use the program. As this process indicates, the creation of a good computer
solution to a problem—in other words, the creation of a good program—requires a great deal of
interaction between the programmer and the user.

Employment Opportunities
When searching for a job in computer programming, you will encounter ads for “computer
programmers” as well as for “computer software engineers.” Although job titles and descriptions
Overview-
vary, computer software engineers typically are responsible for designing an appropriate solution
Programmer to a user’s problem, while computer programmers are responsible for translating the solution
Qualities into a language that the computer can understand. The process of translating the solution is
video called coding.
Keep in mind that, depending on the employer and the size and complexity of the user’s
problem, the design and coding tasks may be performed by the same employee, no matter what
his or her job title is. In other words, it’s not unusual for a software engineer to code her
solution, just as it’s not unusual for a programmer to have designed the solution he is coding.
Programmers and software engineers need to have strong problem-solving and analytical skills,
as well as the ability to communicate effectively with team members, end users, and other
nontechnical personnel. Typically, computer software engineers are expected to have at least a

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Visual Basic 2012

bachelor’s degree in software engineering, computer science, or mathematics, along with


practical work experience, especially in the industry in which they are employed. Computer
programmers usually need at least an associate’s degree in computer science, mathematics, or
information systems, as well as proficiency in one or more programming languages.
Computer programmers and software engineers are employed by companies in almost every
industry, such as telecommunications companies, software publishers, financial institutions,
3
insurance carriers, educational institutions, and government agencies. The Bureau of Labor
Statistics predicts that employment of computer software engineers will increase by 28% from
2010 to 2020. The employment of computer programmers, on the other hand, will increase
by 12% over the same period. In addition, consulting opportunities for freelance programmers
and software engineers are expected to increase as companies look for ways to reduce their
payroll expenses.
There is a great deal of competition for programming and software engineering jobs, so
jobseekers will need to keep up to date with the latest programming languages and technologies.
A competitive edge may be gained by obtaining vendor-specific or language-specific
certifications. More information about computer programmers and computer software
engineers can be found on the Bureau of Labor Statistics Web site at www.bls.gov.

Visual Basic 2012


In this book, you will learn how to create programs using the Visual Basic 2012 programming
language. Visual Basic 2012 is an object-oriented programming language, which is a language
that allows the programmer to use objects to accomplish a program’s goal. An object is anything
that can be seen, touched, or used. In other words, an object is nearly any thing. The objects in
an object-oriented program can take on many different forms. Programs written for the
Windows environment typically use objects such as check boxes, list boxes, and buttons. A
payroll program, on the other hand, might utilize objects found in the real world, such as a time
card object, an employee object, and a check object.
Every object in an object-oriented program is created from a class, which is a pattern that the
computer uses to create the object. The class contains the instructions that tell the computer
how the object should look and behave. An object created from a class is called an instance of
the class and is said to be instantiated from the class. An analogy involving a cookie cutter and
cookies is often used to describe a class and its objects: the class is the cookie cutter, and the
objects instantiated from the class are the cookies. You will learn more about classes and objects
throughout this book.
Visual Basic 2012 is one of the languages included in Visual Studio 2012, which is available
in many different editions. The most robust edition is Visual Studio Ultimate 2012, followed
by Visual Studio Premium 2012, Visual Studio Professional 2012, and then the Express
editions of Visual Studio 2012. Microsoft plans to release four different Express editions:
Visual Studio Express 2012 for Windows 8, Visual Studio Express 2012 for Web, Visual
Studio Express 2012 for Windows Desktop, and Visual Studio Express 2012 for Windows
Phone. Each of these products include an integrated development environment (IDE), which
is an environment that contains all of the tools and features you need to create, run, and
test your programs.
You can use Visual Basic to create programs, called applications, for the Windows environment
or for the Web. A Windows application has a Windows user interface and runs on a personal
computer. A user interface is what the user sees and interacts with while an application is
running. Examples of Windows applications include graphics programs, data-entry systems, and
games. A Web application, on the other hand, has a Web user interface and runs on a server.
You access a Web application using your computer’s browser. Examples of Web applications

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
OVERVIEW An Introduction to Programming

include e-commerce applications available on the Internet, and employee handbook applications
accessible on a company’s intranet. You also can use Visual Basic to create applications for tablet
PCs and mobile devices, such as cell phones and PDAs (personal digital assistants).

A Visual Basic 2012 Demonstration


4
In the following set of steps, you will run a Visual Basic 2012 application that shows you some of
the objects you will learn about in the chapters. For now, it is not important for you to
understand how these objects were created or why the objects perform the way they do. Those
questions will be answered in the chapters.

START HERE To run the Visual Basic 2012 application:


1. Press and hold down the Windows logo key on your keyboard as you tap the letter r.
The Windows
logo key looks
The Run dialog box opens. Release the logo key.
like this: . 2. Click the Browse button to open the Browse dialog box. Locate and then open the
VB2012\Overview folder on your computer’s hard disk or on the device designated by
your instructor.
3. Click Monthly Payment Calculator (Monthly Payment Calculator.exe) in the list of
filenames. (Depending on how Windows is set up on your computer, you may see the
.exe extension on the filename.) Click the Open button. The Browse dialog box closes
and the Run dialog box appears again.
4. Click the OK button in the Run dialog box. After a few moments, the Monthly Payment
Calculator application shown in Figure 1 appears on the screen. The interface contains a
text box, list box, buttons, radio buttons, and labels. You can use the application to
calculate the monthly payment for a car loan.

if you are using Windows 7, if you are using Windows 7,


the title bar text will be these buttons will look
left-aligned different

text box

list box radio buttons

Don’t be
concerned if
labels
some of the
letters on your
screen are
underlined. You can
show/hide the underlined buttons
letters by pressing the
Alt key. Figure 1 Monthly Payment Calculator application

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Using the Chapters Effectively

5. Use the application to calculate the monthly payment for a $20,000 loan at 6.75%
interest for five years. Type 20000 in the Principal text box and then click 6.75 % in
the Interest list box. The radio button corresponding to the five-year term is already
selected, so you just need to click the Calculate button to compute the monthly
payment. The application indicates that your monthly payment would be $393.67.
See Figure 2.
5

Figure 2 Computed monthly payment

6. Now determine what your monthly payment would be if you borrowed $10,000 at 8%
interest for four years. Type 10000 in the Principal text box. Scroll down the Interest list
box and then click 8.00 %. Click the 4 years radio button and then click the Calculate
button. The Monthly payment box shows $244.13.
7. Click the Exit button to close the application.

Using the Chapters Effectively


This book is designed for a beginning programming course. However, it assumes students are
familiar with basic Windows skills and file management. The chapters in this book will help you
learn how to write programs using Microsoft Visual Basic 2012. The chapters are designed to be
used at your computer. Begin by reading the text that explains the concepts. When you come to
the numbered steps, follow the steps on your computer. Read each step carefully and completely
before you try it. As you work, compare your screen with the figures to verify your results. The
figures in this book reflect how your screen will look if you are using Visual Studio Professional
2012 and a Microsoft Windows 8 system. Your screen may appear slightly different in some
instances if you are using a different edition of Visual Studio or if you are using another version
of Microsoft Windows. Don’t worry if your screen display differs slightly from the figures. The
important parts of the screen display are labeled in each figure. Just be sure you have these parts
on your screen.
Do not worry about making mistakes; that’s part of the learning process. Tip notes identify
common problems and explain how to get back on track. They also provide additional Tip notes are
information about a procedure—for example, an alternative method of performing the designated by
procedure. the icon.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
OVERVIEW An Introduction to Programming

Each chapter is divided into three lessons. You might want to take a break between lessons.
Following each lesson is a Summary section that lists the important elements of the lesson. After
the Summary section is a listing of the key terms (including definitions) covered in the lesson.
Following the Key Terms section are questions and exercises designed to review and reinforce
the lesson’s concepts. You should complete all of the end-of-lesson questions and several
exercises before continuing to the next lesson. It takes a great deal of practice to acquire the
6 skills needed to create good programs, and future chapters assume that you have mastered the
information found in the previous chapters.
Some of the end-of-lesson exercises are Discovery exercises, which allow you to both “discover”
the solutions to problems on your own and experiment with material that is not covered in the
chapter. Some lessons also contain one or more Debugging exercises. In programming, the term
debugging refers to the process of finding and fixing any errors, called bugs, in a program.
Debugging exercises provide opportunities for you to find and correct the errors in existing
applications. Appendix A, which can be covered along with Chapter 3, guides you through the
process of locating and correcting a program’s errors (bugs).
Throughout the book you will find GUI (graphical user interface) design tips. These tips contain
guidelines and recommendations for designing applications. You should follow these guidelines
and recommendations so that your applications follow the Windows standards.

Summary
l Programs are the step-by-step instructions that tell a computer how to perform a task.
l Programmers use various programming languages to communicate with the computer.
l The creation of a good program requires a great deal of interaction between the programmer
and the user.
l Programmers rigorously test a program with sample data before releasing the program to
the user.
l It’s not unusual for the same person to perform the duties of both a software engineer and a
programmer.
l An object-oriented programming language, such as Visual Basic 2012, allows programmers
to use objects to accomplish a program’s goal. An object is anything that can be seen,
touched, or used.
l Every object in an object-oriented program is instantiated (created) from a class, which is a
pattern that tells the computer how the object should look and behave. An object is referred
to as an instance of the class.
l The process of locating and correcting the errors (bugs) in a program is called debugging.

Key Terms
Applications—programs created for the Windows environment, the Web, or mobile devices
Class—a pattern that the computer uses to create (instantiate) an object
Coding—the process of translating a solution into a language that the computer can understand
Computer programs—the directions given to computers; also called programs
Debugging—the process of locating and correcting the errors (bugs) in a program
IDE—integrated development environment

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Key Terms

Instance—an object created (instantiated) from a class


Instantiated—the process of creating an object from a class
Integrated development environment—an environment that contains all of the tools and
features you need to create, run, and test your programs; also called an IDE
Object—anything that can be seen, touched, or used
7
Object-oriented programming language—a programming language that allows the programmer
to use objects to accomplish a program’s goal
Programmers—the people who write computer programs
Programming—the process of giving a mechanism the directions to accomplish a task
Programming languages—languages used to communicate with a computer
Programs—the directions given to computers; also called computer programs
User interface—what the user sees and interacts with while an application is running

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CHAPTER 1
An Introduction to
Visual Basic 2012

Creating a Splash Screen

In this chapter, you will use Visual Basic 2012, Microsoft’s newest
version of the Visual Basic language, to create a splash screen for the
Red Tree Inn. A splash screen is the first image that appears when an
application is started. It is used to introduce the application and to hold
the user’s attention while the application is being read into the
computer’s internal memory.

All Microsoft screenshots used with permission from Microsoft Corporation.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CHAPTER 1 An Introduction to Visual Basic 2012

Previewing the Splash Screen


Before you start the first lesson in this chapter, you will preview a completed splash screen.
The splash screen is contained in the VB2012\Chap01 folder.
START HERE To preview a completed splash screen:
10 1. Press and hold down the Windows logo key on your keyboard as you tap the letter r.
The Run dialog box opens. Release the logo key.
The Windows
logo key looks 2. Click the Browse button to open the Browse dialog box. Locate and then open the
like this: . VB2012\Chap01 folder on your computer’s hard disk or on the device designated by
your instructor.
You will learn 3. Click RTI Splash (RTI Splash.exe) in the list of filenames. (Depending on how
more about Windows is set up on your computer, you may see the .exe extension on the
splash screens filename.) Click the Open button. The Browse dialog box closes and the Run dialog
(such as how to box appears again.
round their
corners) in Lesson C’s 4. Click the OK button in the Run dialog box. After a few moments, the splash screen
Discovery Exercises 4, 5, shown in Figure 1-1 appears on the screen. The splash screen closes when six seconds
and 6. have elapsed.

Figure 1-1 Splash screen for the Red Tree Inn


Photo courtesy of Diane Zak

Chapter 1 is designed to help you get comfortable with the Visual Studio 2012 integrated
development environment. As you learned in the Overview, an integrated development
environment (IDE) is an environment that contains all of the tools and features you need to
create, run, and test your programs. As do all the chapters in this book, Chapter 1 contains three
lessons. You should complete a lesson in full and do all of the end-of-lesson questions and
several exercises before continuing to the next lesson.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
The Splash Screen Application LESSON A

& LESSON A
After studying Lesson A, you should be able to:
l Start and customize Visual Studio 2012
l Create a Visual Basic 2012 Windows application
l Manage the windows in the IDE 11

l Set the properties of an object


l Restore a property to its default setting
l Save a solution
l Close and open an existing solution

The Splash Screen Application


In this chapter, you will create a splash screen using Visual Basic 2012. The following set of steps
will guide you in starting Visual Studio Professional 2012 from either Windows 8 or Windows 7.
Your steps may differ slightly if you are using a different edition of Visual Studio 2012.
To start Visual Studio Professional 2012: START HERE

1. Windows 8: If necessary, tap the Windows logo key to switch to the Windows 8
tile-based mode, and then click the Visual Studio 2012 tile.
Windows 7: Click the Start button on the Windows 7 taskbar and then point to All
Programs. Click Microsoft Visual Studio 2012 on the All Programs menu and then
click Visual Studio 2012.
The Ch01A
2. If the Choose Default Environment Settings dialog box appears, click Visual Basic video
Development Settings and then click Start Visual Studio. demon-
strates all of
3. Click WINDOW on the menu bar, click Reset Window Layout, and then click the Yes
the steps contained in
button. When you start Visual Studio Professional 2012, your screen will appear similar Lesson A. You can view
to Figure 1-2. However, your menu bar may contain underlined letters, called access the video either before or
keys. You will learn about access keys in Chapter 2. (You can show/hide the access keys after completing the
by pressing the Alt key on your keyboard.) lesson.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CHAPTER 1 An Introduction to Visual Basic 2012

Solution Explorer
Toolbox window’s tab Start Page window window

12

Team Explorer
window’s tab

be sure these check


boxes are selected

Figure 1-2 Microsoft Visual Studio Professional 2012 startup screen

Note: To select a different window layout, click TOOLS on the menu bar, click Import and
Export Settings, select the Reset all settings radio button, click the Next button, select the
appropriate radio button, click the Next button, click the settings collection you want to use,
and then click the Finish button.

Next, you will configure Visual Studio so that your screen agrees with the figures and tutorial
steps in this book. As mentioned in the Overview, the figures reflect how your screen will look if
you are using Visual Studio Professional 2012 and a Microsoft Windows 8 system. Your screen
may appear slightly different in some instances if you are using a different edition of Visual
Studio or if you are using another version of Microsoft Windows. Don't worry if your screen
display differs slightly from the figures.
START HERE To configure Visual Studio:
1. Click TOOLS on the menu bar and then click Options to open the Options dialog box.
Click the Projects and Solutions node. Use the information shown in Figure 1-3 to
select and deselect the appropriate check boxes. (Your dialog box will look slightly
different if you are using Windows 7. For example, the title bar text will be left-aligned
rather than centered, and the buttons on the title bar will look different.) When you are
finished, click the OK button to close the Options dialog box.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
The Splash Screen Application LESSON A

the title bar text will be these buttons will look


left-aligned in Windows 7 different in Windows 7

13

select these five


check boxes
deselect these
three check boxes

Projects and
Solutions node

Figure 1-3 Options dialog box

The splash screen will be a Windows application, which means it will have a Windows user
interface and run on a desktop computer. Recall that a user interface is what the user sees and
interacts with while an application is running. Windows applications in Visual Basic are
composed of solutions, projects, and files. A solution is a container that stores the projects and
files for an entire application. Although the solutions in this book contain only one project, a
solution can contain several projects. A project also is a container, but it stores only the files
associated with that particular project.
To create a Visual Basic 2012 Windows application: START HERE

1. Click FILE on the menu bar and then click New Project to open the New Project
dialog box.
2. If necessary, expand the Visual Basic node in the Installed Templates list, and then
(if necessary) click Windows.
3. If necessary, click Windows Forms Application in the middle column of the
dialog box.
4. Change the name entered in the Name box to Splash Project.
5. Click the Browse button to open the Project Location dialog box. Locate and then
click the VB2012\Chap01 folder. Click the Select Folder button to close the Project
Location dialog box.
6. If necessary, select the Create directory for solution check box in the New Project
dialog box. Change the name entered in the Solution name box to Splash Solution.
Figure 1-4 shows the completed New Project dialog box in Visual Studio Professional
2012. (Your dialog box will look slightly different if you are using Windows 7 or a
different edition of Visual Studio.) The drive letter may be different from the one shown

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CHAPTER 1 An Introduction to Visual Basic 2012

in the figure if you are saving to a device other than your computer’s hard drive—for
example, if you are saving to a flash drive.

14

your drive letter


might be different

Figure 1-4 Completed New Project dialog box in Visual Studio Professional 2012

7. Click the OK button to close the New Project dialog box. The computer creates a
solution and adds a Visual Basic project to the solution. The names of the solution and
project, along with other information pertaining to the project, appear in the Solution
Explorer window. See Figure 1-5. In addition to the windows shown earlier in Figure 1-2,
three other windows appear in the IDE: Windows Form Designer, Properties, and Data
Sources. (Don’t be concerned if different properties appear in your Properties window.)

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
The Splash Screen Application LESSON A

if you are using Windows 7,


Form1 will be left-aligned Auto Hide button

15

If you want
solution and to size the
Data Sources project names Solution Explorer
window’s tab and information window to match
Figure 1-5,
position your mouse
pointer on the window’s
left border until the
Properties window mouse pointer becomes
a sizing pointer
(a horizontal line with
an arrowhead at each
end), and then drag the
border to either the left
or the right.

Windows Form
Designer window

Figure 1-5 Solution and Visual Basic project

Managing the Windows in the IDE


In most cases, you will find it easier to work in the IDE if you either close or auto-hide the
windows you are not currently using. The easiest way to close an open window is to click the
Close button on the window’s title bar. In most cases, the VIEW menu provides an appropriate
option for opening a closed window. Rather than closing a window, you also can auto-hide it.
You auto-hide a window using the Auto Hide button (refer to Figure 1-5) on the window’s title
bar. The Auto Hide button is a toggle button: clicking it once activates it, and clicking it again
deactivates it. The Toolbox and Data Sources windows in Figure 1-5 are auto-hidden windows.
To close, open, auto-hide, and display windows in the IDE: START HERE

1. Click the Close button on the Properties window’s title bar to close the window. Now,
click VIEW on the menu bar and then click Properties Window to open the window.
2. If your IDE contains the Team Explorer window, click the Team Explorer tab and then
click the Close button on the window’s title bar.
3. Click the Auto Hide (vertical pushpin) button on the Solution Explorer window. The
Solution Explorer window is minimized and appears as a tab on the edge of the IDE.
4. To temporarily display the Solution Explorer window, click the Solution Explorer tab.
Notice that the Auto Hide button is now a horizontal pushpin rather than a vertical

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CHAPTER 1 An Introduction to Visual Basic 2012

pushpin. To return the Solution Explorer window to its auto-hidden state, click the
Solution Explorer tab again.
5. To permanently display the Solution Explorer window, click the Solution Explorer tab
and then click the Auto Hide (horizontal pushpin) button on the window’s title bar. The
vertical pushpin replaces the horizontal pushpin on the button.
16 6. If necessary, close the Data Sources window.
7. Figure 1-6 shows the current status of the windows in the IDE. Only the Windows Form
Designer, Solution Explorer, and Properties windows are open; the Toolbox window is
auto-hidden. If necessary, click Form1.vb in the Solution Explorer window. If the items
in the Properties window do not appear in alphabetical order, click the Alphabetical
button.

To reset the Alphabetical button


window layout in
the IDE, click
WINDOW on the
menu bar, click
Reset Window Layout,
and then click the Yes
button.

Figure 1-6 Current status of the windows in the IDE

In the next several sections, you will take a closer look at the Windows Form Designer, Solution
Explorer, and Properties windows. (The Toolbox window is covered in Lesson B.)

The Windows Form Designer Window


Figure 1-7 shows the Windows Form Designer window, where you create (or design) your
application’s graphical user interface, more simply referred to as a GUI. Only a Windows Form
object appears in the designer window shown in the figure. A Windows Form object, or form, is
the foundation for the user interface in a Windows application. You create the user interface by
adding other objects, such as buttons and text boxes, to the form. Notice that a title bar appears
at the top of the form. The title bar contains a default caption (Form1) along with Minimize,
Maximize, and Close buttons. (The title bar text will be left-aligned in Windows 7.) At the top of

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
The Splash Screen Application LESSON A

the designer window is a tab labeled Form1.vb [Design]. Form1.vb is the name of the file (on
your computer’s hard disk or on another device) that contains the Visual Basic instructions
associated with the form, and [Design] identifies the window as the designer window.

name of the disk file


that contains the instructions title bar (Form1 will be left-aligned
associated with the form if you are using Windows 7) 17

form

Figure 1-7 Windows Form Designer window

As you learned in the Overview, all objects in an object-oriented program are instantiated Recall that a
(created) from a class. A form, for example, is an instance of the Windows Form class. The form class is a pattern
that the
is automatically instantiated for you when you create a Windows application.
computer uses
to create an
object.
The Solution Explorer Window
The Solution Explorer window displays a list of the projects contained in the current solution and the
items contained in each project. Figure 1-8 shows the Solution Explorer window for the Splash Solution,
which contains one project named Splash Project. Within the Splash Project are the My Project folder
and two files named App.config and Form1.vb. The project also contains other items, which are
typically kept hidden. However, you can display the additional items by clicking the Show All Files
button. You would click the button again to hide the items. The .vb on the Form1.vb filename indicates
that the file is a Visual Basic source file. A source file is a file that contains program instructions, called
code. The Form1.vb file contains the code associated with the form displayed in the designer window.
You can view the code using the Code Editor window, which you will learn about in Lesson B.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CHAPTER 1 An Introduction to Visual Basic 2012

Show All Files button


18

Figure 1-8 Solution Explorer window

The Form1.vb source file is referred to as a form file because it contains the code associated with
a form. The code associated with the first form included in a project is automatically stored in a
form file named Form1.vb. The code associated with the second form in the same project is
stored in a form file named Form2.vb, and so on. Because a project can contain many forms and,
therefore, many form files, it is a good practice to give each form file a more meaningful name.
Doing this will help you keep track of the various form files in the project. You can use the
Properties window to change the filename.

The Properties Window


As is everything in an object-oriented language, a file is an object. Each object has a set of
attributes that determine its appearance and behavior. The attributes are called properties and
are listed in the Properties window. When an object is created, a default value is assigned to each
of its properties. The Properties window shown in Figure 1-9 lists the default values assigned to
the properties of the Form1.vb file. (You do not need to widen your Properties window to match
Figure 1-9.) As indicated in the figure, the Properties window includes an Object box and a
Properties list. The Object box contains the name of the selected object. In this case, it contains
Form1.vb, which is the name of the form file. The Properties list has two columns. The left
To display the
column displays the names of the selected object’s properties. You can use the Alphabetical and
properties of the
Form1.vb form
Categorized buttons to display the names either alphabetically or by category, respectively.
file, Form1.vb However, it’s usually easier to work with the Properties window when the properties are listed in
must be alphabetical order, as they are in Figure 1-9. The right column in the Properties list is called the
selected in the Solution Settings box and displays the current value (or setting) of each of the object’s properties. A brief
Explorer window. description of the selected property appears in the Description pane.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
The Splash Screen Application LESSON A

Categorized button

Object box
Alphabetical button

19
Settings box
Properties list

Description pane

Figure 1-9 Properties window

To use the Properties window to change the form file’s name: START HERE

1. Form1.vb should be selected in the Solution Explorer window. Click File Name in the
Properties list and then type Splash Form.vb. Be sure to include the .vb extension on You also can
the filename; otherwise, the computer will not recognize the file as a source file. change the File
Name property
2. Press Enter. Splash Form.vb appears in the Solution Explorer and Properties windows by right-clicking
and on the designer window’s tab, as shown in Figure 1-10. Form1.vb in the
Solution Explorer window
and then clicking Rename
on the context menu.
form file’s name

form file’s name

Figure 1-10 Form file’s name shown in various locations

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CHAPTER 1 An Introduction to Visual Basic 2012

Properties of a Windows Form


Like a file, a Windows form also has a set of properties. The form’s properties will appear in the
Properties window when you select the form in the designer window.
START HERE To view the properties of the form:
20 1. Click the form in the designer window to display the form’s properties in the Properties
window. Figure 1-11 shows a partial listing of the properties of a Windows form.

location of the
form name Form class class name

Figure 1-11 Properties window showing a partial listing of the form’s properties

Notice that Form1 System.Windows.Forms.Form appears in the Object box in Figure 1-11.
Form1 is the name of the form. The name is automatically assigned to the form when the form
is instantiated (created). In System.Windows.Forms.Form, Form is the name of the class used to
instantiate the form. System.Windows.Forms is the namespace that contains the Form class
definition. A class definition is a block of code that specifies (or defines) an object’s appearance
and behavior. All class definitions in Visual Basic 2012 are contained in namespaces, which you
can picture as blocks of memory cells inside the computer. Each namespace contains the code
that defines a group of related classes. The System.Windows.Forms namespace contains the
definition of the Windows Form class. It also contains the class definitions for objects you add to
a form, such as buttons and text boxes.
The period that separates each word in System.Windows.Forms.Form is called the dot
member access operator. Similar to the backslash (\) in a folder path, the dot member access
operator indicates a hierarchy, but of namespaces rather than folders. In other words, the
backslash in the path D:\VB2012\Chap01\Splash Solution\Splash Project\Splash Form.vb
indicates that the Splash Form.vb file is contained in (or is a member of) the Splash Project
folder, which is a member of the Splash Solution folder, which is a member of the Chap01
folder, which is a member of the VB2012 folder, which is a member of the D: drive. Likewise, the
name System.Windows.Forms.Form indicates that the Form class is a member of the Forms

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Properties of a Windows Form LESSON A

namespace, which is a member of the Windows namespace, which is a member of the


System namespace. The dot member access operator allows the computer to locate the Form
class in the computer’s internal memory, similar to the way the backslash (\) allows the
computer to locate the Splash Form.vb file on your computer’s disk.

The Name Property 21


As you do to a form file, you should assign a more meaningful name to a Windows form because
doing so will help you keep track of the various forms in a project. Unlike a file, a Windows form
has a Name property rather than a File Name property. You use the name entered in an object’s
Name property to refer to the object in code, so each object must have a unique name. The
name you assign to an object must begin with a letter and contain only letters, numbers, and the
underscore character. The name cannot include punctuation characters or spaces.
There are several conventions for naming objects in Visual Basic. In this book, you will use a
naming convention called Hungarian notation. Names in Hungarian notation begin with a three
(or more) character ID that represents the object’s type, with the remaining characters in the
name representing the object’s purpose. For example, using Hungarian notation, you might
assign the name frmSplash to the current form. The “frm” identifies the object as a form, and
“Splash” reminds you of the form’s purpose. Hungarian notation names are entered using camel
case, which means you enter the ID characters in lowercase and then capitalize the first letter of
each subsequent word in the name. Camel case refers to the fact that the uppercase letters
appear as “humps” in the name because they are taller than the lowercase letters.
To change the name of the form: START HERE

1. Drag the scroll box in the Properties window to the top of the vertical scroll bar. As you
scroll, notice the various properties associated with a form. Also notice that the items
within parentheses appear at the top of the Properties list.
2. Click (Name) in the Properties list. Type frmSplash and press Enter. An asterisk (*)
appears on the designer window’s tab. The asterisk indicates that the form has been
changed since the last time it was saved.

The Text Property


In addition to changing the form’s Name property, you also should change its Text property, which
controls the text displayed in the form’s title bar. Form1 is the default value assigned to the Text
property of the first form in a project. In this case, “Red Tree Inn” would be a more descriptive value.
To set the Text property of the form: START HERE

1. Scroll down the Properties window until you see the Text property in the Properties list
and then click Text.
2. Type Red Tree Inn and press Enter. The new text appears in the property’s Settings
box and also in the form’s title bar.

The Name and Text properties of a Windows form should always be changed to more
meaningful values. The Name property is used by the programmer when coding the application.
The Text property, on the other hand, is read by the user while the application is running.

The StartPosition Property


When an application is started, the computer uses the form’s StartPosition property to
determine the form’s initial position on the screen. The frmSplash form represents a splash
screen, which typically appears in the middle of the screen.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CHAPTER 1 An Introduction to Visual Basic 2012

START HERE To center a form on the screen when the application is started:
1. Click StartPosition in the Properties list and then click the list arrow in the
Settings box.
2. Click CenterScreen in the list.

22
The Font Property
A form’s Font property determines the type, style, and size of the font used to display the text
on the form. A font is the general shape of the characters in the text. Segoe UI, Tahoma, and
Microsoft Sans Serif are examples of font types. Font styles include regular, bold, and italic.
The numbers 9, 12, and 18 are examples of font sizes, which typically are measured in points,
with one point equaling 1 ⁄ 72 of an inch. The recommended font for applications created for
systems running Windows 8, Windows 7, or Windows Vista is Segoe UI because it offers
improved readability. Segoe is pronounced SEE-go, and UI stands for user interface. For most
of the elements in the interface, you will use a font size of 9-point. However, to make the
figures in the book more readable, some of the interfaces created in this book will use a larger
font size.
START HERE To set the form’s Font property:
1. Click Font in the Properties list and then click the … (ellipsis) button in the Settings box
to open the Font dialog box.
2. Locate and then click the Segoe UI font in the Font box. Click 9 in the Size box and then
click the OK button. (Don’t be concerned if the size of the form changes.)

The Size Property


As you can with any Windows object, you can size a form by selecting it and then 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 your
keyboard. In addition, you can set the object’s Size property.
START HERE To set the form’s Size property:
1. Click Size in the Properties list. Notice that the Size property contains two numbers
separated by a comma and a space. The first number represents the width of the form,
measured in pixels. The second number represents the height, also measured in pixels.
A pixel, which is short for “picture element,” is one spot in a grid of thousands of such
spots that form an image either produced on the screen by a computer or printed on a
page by a printer.
2. Type 605, 334 in the Size property’s Settings box and press Enter. Expand the Size
property by clicking the plus box that appears next to the property. Notice that the first
number listed in the property represents the width, and the second number represents
the height. Click the minus box to collapse the property.

Setting and Restoring a Property’s Value


In the next set of steps, you will practice setting and then restoring a property’s value. More
specifically, you will set and then restore the value of the form’s BackColor property, which
determines the background color of the form.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Saving a Solution LESSON A

To set and then restore the form’s BackColor property value: START HERE

1. 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.
2. 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. Figure 1-12 shows 23
the status of the form in the IDE.

the asterisk indicates that the if you are using Windows 7,


form has been changed since the text in the title bar will be
the last time it was saved left-aligned

Figure 1-12 Status of the form in the IDE

Saving a Solution
The asterisk (*) that appears on the designer tab in Figure 1-12 indicates that a change was made
to the form since the last time it was saved. It is a good idea to save the current solution every 10
The Save All
or 15 minutes so that you will not lose a lot of your work if the computer loses power. You can button on the
save the solution by clicking FILE on the menu bar and then clicking Save All. You also can click Standard toolbar
the Save All button on the Standard toolbar. When you save the solution, the computer saves looks like this:
any changes made to the files included in the solution. It also removes the asterisk that appears .
on the designer window’s tab.
To save the current solution: START HERE

1. Click FILE on the menu bar and then click Save All. The asterisk is removed from the
designer window’s tab, indicating that all changes made to the form have been saved.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CHAPTER 1 An Introduction to Visual Basic 2012

Closing the Current Solution


When you are finished working on a solution, you should close it. Closing a solution closes all
projects and files contained in the solution.
START HERE To close the Splash Solution:
24 1. Click FILE on the menu bar. Notice that the menu contains a Close option and a Close
Solution option. The Close option closes the designer window in the IDE; however, it
does not close the solution itself. Only the Close Solution option closes the solution.
2. Click Close Solution. The Solution Explorer window indicates that no solution is
currently open in the IDE.

Opening an Existing Solution


You can use the FILE menu to open an existing solution. The names of solution files end with
.sln. If a solution is already open in the IDE, you will be given the option of closing it before
another solution is opened.
START HERE To open the Splash Solution:
1. Click FILE on the menu bar and then click Open Project to open the Open Project
dialog box.
2. Locate and then open the VB2012\Chap01\Splash Solution folder. Click Splash
Solution (Splash Solution.sln) in the list of filenames and then click the Open button.
3. The Solution Explorer window indicates that the solution is open. If the designer
window is not open, right-click Splash Form.vb in the Solution Explorer window and
then click View Designer.

Exiting Visual Studio 2012


Finally, you will learn how to exit Visual Studio 2012. You will complete the splash screen in the
remaining two lessons. You can exit Visual Studio using either the Close button on its title bar
or the Exit option on its FILE menu.
START HERE To exit Visual Studio 2012:
1. Click FILE on the menu bar and then click Exit.

Lesson A Summary
l To start Visual Studio 2012:
If you are using Windows 8, tap the Windows logo key (if necessary) to switch to the
Windows 8 tile-based mode and then click the Visual Studio 2012 tile. If you are using
Windows 7, click the Start button on the Windows 7 taskbar, point to All Programs, click
Microsoft Visual Studio 2012, and then click Visual Studio 2012.
l To select a different window layout:
Click TOOLS on the menu bar, click Import and Export Settings, select the Reset all settings
radio button, click the Next button, select the appropriate radio button, click the Next
button, click the settings collection you want to use, and then click the Finish button.

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Random documents with unrelated
content Scribd suggests to you:
A RETORT FROM THE CATBIRD

I heard the Catbird in the bush


With breathless ecstasy;
No bobolink or fluttering thrush
Made carol sweet as he.

It bubbled like a mountain rill


Drenching the weary day,
With eddying turn and rippling trill,
A magic roundelay.

I heard the Catbird once again.


A harsh, discordant note,
Which pierced the shuddering ear with pain,
Came from the selfsame throat.

O bird perverse! That heavenly voice


Tuned to so sharp a key!
Why cease to make the air rejoice
Debasing minstrelsy?

Why not be ever at your best?


Again the peevish mew
Answering, accusing me with zest:
"Are you?" he cried, "are y-o-u?"

Abbie Farwell Brown.


WOODPECKER LIFE

Margaret Coulson Walker

On the thirteenth of July a red-mutched woodpecker knocked on


the stricken bough of a lofty elm to crave of the Dryad within
hospitality for a season. Yes, her wish would be granted, but only on
condition that she would dig out a shelter for herself there in the
hard, dry wood.
What had gone wrong in the woodpecker family that she was in
need of shelter this late in the year? Earlier in the summer she and
her mate had burrowed out a comfortable home in a great oak tree
not two hundred yards away. Then they were on the best of terms
and had relieved each other at the task of digging out their dwelling
place. Twenty or twenty-five minutes at a time was thought long
enough for either of them to devote to so labourious a task in the
springtime; then the other spent an equal time at the work, while
the one off duty hurried away to partake of refreshments or to seek
rest in change of occupation.
Then there seemed to be some joy in their lives, for when they
had occasionally found time for recreation, they had chased each
other around the tree trunks and given utterance to their enjoyment
of the game in many a peal of cackling laughter. Near the base of a
tree the game began, and, spirally round and round its trunk, they
pursued each other, the one in the lead every now and then casting
a challenging look behind, then hurrying upward faster than before.
Their playtimes were brief, however, for the unfinished burrow was
calling.
When this was completed and later a half dozen or more eggs
were laid, though madam spent most of her time in dispensing
warmth to them, her mate also did his share. Together they had
devoted their energies to providing for the little ones that pecked
their way out of the round, white eggs. Many long journeys were
they compelled to take, and many were the hours spent in search of
suitable food for their hungry offspring; but on their return their
throats were always full to the brim with the nourishment which they
pumped into infant throats as, hanging head downward over them,
they clung with their claws to the entrance of their home. And when,
after a time, the chicks were old enough to scramble about on the
trunk of the tree outside their home, a wheezy call from one of them
was enough to bring one or both of the parents, with throat
distended with the best the wood afforded, to minister to their
wants. Together they had driven away the over-solicitous squirrels
and meddlesome sparrows who came to visit them. Together they
had guided their asthmatic young family about the wood, teaching
them by example, if not by precept, where food was to be found,
and how to meet the dangers they were likely to encounter at any
moment.
The accidents of nature had depleted the brood, till now but two
of them were left. A ball of baby feathers in the home of an owl
living in the wood told the story of the passing of one of them; the
gladness which attended the home-coming of a foraging mother
squirrel marked the taking off of another; so they had gone, till only
these two remained, wheezy and exacting.
Of late the care of them had fallen mainly on the father, who
picked up a living for them as best he could. At times he seemed to
try to get away from them—a futile effort, for when they did not
follow his undulating flight in their awkward up-and-down fashion,
they went in search of him if he was gone a few minutes overtime.
Here on the thirteenth of July was the mother seeking shelter
away from her former home. Had there been a family disagreement?
Was the home-nest no longer large enough for the parent birds and
their now almost grown-up family? Was she planning for a new
brood? Surely not! It would be impossible to rear in a single season
two broods requiring so much care.
Whatever her purpose, here she was, drawing her plans on the
under side of the dry old bough. Soon she began to peck out an
entrance, and it was not long before the chips were flying in every
direction. More than an hour she worked, then flew to the dead top
of a tree across the way, where she sat for a brief time resting and
sunning herself. Twice she left her perch to dart out after passing
insects, then returned to her labour. Occasionally she swung around
to the top side of the dead branch, and tore off bits of bark either
for the purpose of seeing if the hole was going clear through or for
securing the insect fare lurking under it. This part of the work
continued at intervals, till the bark was removed from all the
excavated portion of the bough. All day, until about five o'clock, she
spent at her task with but little rest, then there was a long visit to
the rest perch in the neighbouring treetop.
The early morning hours were probably devoted to commissary
tours; for it was almost eight o'clock when she appeared on the
scene of her labours and again began to wield the pick. About ten
o'clock her spouse appeared and arranged himself comfortably on
the same limb about a foot away from the hole she was digging, but
not by so much as a single stroke did he assist her. Soon a wheezy,
whistling cry called him to duties as insistent as home building, and
he departed.
After watching the progress of woodpecker affairs for some time,
a dweller in the house under the tree decided to lend a hand. A
worm-eaten hitching-post stood near, on which was placed pieces of
bread for the hungry little wielder of the pick. This not only satisfied
her wants, but served also to bring her mate and offspring near
occasionally. At first the young members of the family refused to pick
up this food set before them, but, instead, clung to a neighbouring
tree and called vociferously for help. Then the father took the bits of
bread and pushed them far down into the screaming throats. The
young Romulus must have possessed wonderful powers of
endurance if the woodpeckers of old ministered to him as vigorously
in response to his infantile wails as the woodpeckers of to-day
respond to the screaming demands of their own offspring. How
gentle the wolf must have seemed in comparison!
Several times the young woodpeckers followed the father to the
limb in which the mother was chiseling a home. Together they
watched her work, but during the first three days seemed to take no
interest whatever in the hole she was making. Then the father went
in and examined the opening, but flew away without giving any real
aid. And all through the work his assistance seemed to be limited to
inspection.
In her digging, the mother woodpecker clung with her claws to
the opening of the burrow, and, head downward, pecked rapidly.
Sometimes she would throw out chips—which were little more than
coarse sawdust—after three or four blows; again, she worked for a
minute or two, then threw out several billfuls at a time. In throwing
out these chips she slipped backward and forward over the lower
edge of the opening, after the manner of that old-fashioned toy
called a "supple jack." First she threw her chips to one side, then to
the other, till the ground beneath the burrow, for a space thirty feet
in circumference, was generously sprinkled with them.
Though several persons were watching her, and though squirrels
were springing about among the branches, she was not disturbed,
but went steadily on with her task. While she was away on short
vacations, the wren, dwelling in the porch roof beneath, frequently
investigated the hole she was digging—sparrows examined it, and
squirrels looked into it, but it was very noticeable that they all had
an eye on her return. Once, in her absence, one of her own young
woodpeckers scrambled to the edge of the hole, and peeped in for a
moment, then scuttled back again to the place where the dead
branch joined the trunk of the tree, and, in his usual noisy manner,
demanded food.
It was near the end of the third day's labour that the woodpecker
was first seen "trying on" her new home. Then she went into it, and,
nestling there, with head up for the first time, looked out of the
window. Evidently, the pocket was neither deep enough nor wide
enough, for after this she worked on both bottom and sides of it,
scattering chips as before. The work periods were shorter now and
the rests more frequent, showing that her strength was failing. On
the afternoon of the fifth day, when the burrow was finished,
completely exhausted, she made her way to the roof of the house,
where, with wings spread, she lay for more than an hour. Seemingly
too tired to reach her usual resting place in the treetop across the
way, she lay there gathering strength for the longer flight.
Though the sexes are alike in the redhead family, it was not
difficult to distinguish them in this case, for the feathers about the
head and neck of the mother were much more worn than those of
her less industrious mate. Yet it may be an injustice to him to accuse
him of indolence, for was he not purveying to their younglings?—a
task which may have taxed his energies to the limit. Perhaps, after
all, it was only a case of division of labour.
After the completion of the burrow, though the woodpecker was
anxiously watched for, for several days, she was not seen near it
again, though the usual bits of bread placed on the hitching-post
brought her to its neighbourhood.
The experiment was tried of putting some of the crusts on the top
of the post and stuffing others tightly into the large worm-holes. The
latter were invariably taken first. Though the young birds came there
regularly to be fed, more than a week passed before they made the
slightest effort to help themselves. They would cling to the sides of
the post, and, with upward-pointing, open bills, whistle asthmatically
for the food, which the parents were compelled to place in their
throats. Whether it was wilfulness or inability that caused them to
act as they did, it was impossible to determine.
The whistling of the young birds, which was once believed to
predict rain, or to be a demand for it from a thirsty throat, always
precedes or accompanies the taking of food. It is, doubtless, a little
more frequent before showers, for at such times the older birds are
able to collect more beetles and other insects that come out then
from their shelters into the open.
The old belief that woodpeckers are ever athirst because of their
inability to drink any save the rain that falls into their open throats or
the drops that fall from the leaves, may have some foundation. In
the case of this family, though a basin of water was always
conveniently near, and though sparrows, robins, bluejays, and wrens
constantly patronized it, no woodpecker was ever seen to refresh
himself from it—many as there were of them in the vicinity.
When more bread than the four birds could consume was placed
in the post, the older ones carried a part of it away—usually the
larger pieces on top—for future use, or pounded it tightly into worm-
holes in the same post, but never into the ones in which they found
it.
Several weeks after the burrow was finished, one evening just
about sunset, a redhead was seen peeping from the window in the
treetop; then it was drawn back, and again it appeared and was
withdrawn to be seen no more during the evening. It was a
dormitory, then, that you hollowed out for yourself, was it, my lady?
One morning, near the close of August, it was noticed that the
entrance to the lodging was distinctly larger, and that a patch of
daylight showed through from the other side. Whether, for some
reason, the bird herself had enlarged the opening before departing
for the South, or whether this had been done by mischievous
squirrels on murder bent, is not known; but certain it is that the red-
mutched labourer was gone. Others of her kind lingered in the grove
for a week or more, and though food was placed on the accustomed
post, neither she nor any of her immediate family appeared to claim
it.
When he is gone, the most accomplished songsters are not missed
more than the red-headed woodpecker, whose broad patches of
clear colour enliven the wood. Though he may no longer assist in
the growth of the forests by bringing refreshing showers, as he is
said to have done in the long time ago, he certainly is doing much in
his own way to preserve them. Well might the ancients have made a
god of him. He still possesses one of the gifts which won that
honour for him—the power of producing thunder—and in a way that
mortals can understand. Hear it rumbling among the dead treetops,
as the bird drums rapidly on the dry wood and sets it to vibrating,
then quickly lays his hollow bill against it to add resonance to the
peal. Vulcan himself could not have felt greater satisfaction than he,
as he stops to listen, in conscious pride over his accomplishment.
Whether he is a god made manifest in feathers, or merely an old
woman under a curse, expiating the crime of selfishness in picking
up a living where there seems to be no life, and in sharing this scant
fare with the hungry, as we see this bird with breast flattened and
shoulders bent by hard work, while our sympathies are awakened,
we bless the day that gave to the world this tireless little labourer of
the woods.
KINGFISHER'S NECKLACE AND
RUFFLE

Kingfisher is very proud, indeed, of his white collar and ruffled


head-dress, but there was a time in the long, long ago when he had
neither of these ornaments. He wore a plain suit of gray-blue
feathers and his head was as smooth as a robin's.
In that far-off time Kingfisher lived near a large lake, which was
bordered by long stretches of pine trees. He chose this place for a
home because he could catch plenty of fish in the clear waters of the
lake. Also, he had made a friend of Wolf, who lived with the great
spirit, Manabozho, in a bear-skin wigwam, which stood on the shore.
Wolf was a mighty hunter and provided Manabozho with plenty of
food. It happened one season that game was scarce in the forest
near the wigwam, and Wolf decided to hunt in the woods on the
opposite side of the lake.
"Brother Wolf," said Manabozho, "see how dense the pine woods
over there are. No hunter has ever ventured into that tangled
forest."
"That is why I shall surely find plenty of game there," answered
Wolf.
Accordingly, early next morning Wolf ran around the long margin
of the lake until he came to the thick forest. He soon caught all the
game he could carry, but instead of returning with it to the lodge, he
stopped to fish on Big Rock, which jutted out into the lake.
Kingfisher, perched on one of the tall pine trees, called out: "Wolf,
do not fish from Big Rock. The sea-serpents are lurking near, and
they will catch you."
"I want some fine fish to take to Manabozho," answered Wolf. "I'm
not afraid of the sea-ser——!"
He had not finished speaking when, in a very mysterious way,
something gave his fishing-line a mighty jerk, and Wolf was pulled
headlong into the water.
Manabozho had no game for supper. All night he listened for the
footsteps of his faithful hunter, but Wolf did not return to the lodge.
In the morning the great spirit began to search for his companion.
He traveled all around the long margin of the lake, but not a single
trace of Wolf could he find. Near Big Rock, on a tall pine tree, sat
Kingfisher. Manabozho had never before spoken to the plain little
bird, who was very much surprised when the great spirit said,
"Kingfisher, can you tell me what has happened to Manabozho's
brother Wolf? I'll give you a beautiful necklace of wampum if you
can help me find him."
Kingfisher flew down from the pine tree to a branch near the great
spirit and said, "Yesterday I saw your brother Wolf fishing from Big
Rock. A sea-serpent pulled him under the water. If you would rescue
him you must watch on this side of the lake. When the sun is
highest the sea-serpents come to the rocks to sun themselves."
Manabozho was so pleased with the information that he put a
necklace of beautiful white wampum around Kingfisher's neck.
"You must not tell the serpents that I am watching for Wolf," said
Manabozho.
But Kingfisher was looking in the mirror of the lake, admiring his
new necklace, so he did not hear the great spirit's words.
Manabozho became suspicious and seized the little bird by the head.
Kingfisher wriggled and twisted, and finally freed himself from the
hand of the angry Manabozho and flew away. But the feathers on
Kingfisher's head were very much ruffled in the struggle, and he has
worn them so ever since; also, to this day, he wears Manabozho's
gift of the beautiful white necklace.
OWL WISDOM

Frances Wright

Once upon a time the owls were the largest and the most dull and
stupid of all the birds of the air. While the eagle soared above the
mountain's crest to hail the sun before his rising, and the lark
carolled his matin in the blue fields of ether, the owls were snoring;
when the thrush and the blackbird, retreating from the heat of noon,
filled the deep groves with their melody, the owls snored out the
sylvan concert; and when the soft cushat poured his evening tale of
love into the ear of his listening mate, the owls were still snoring in
their unbroken and dreamless sleep.
It chanced, most naturally, that when towards midnight, the
heavy, big-headed creatures half-opened their stupid eyes, and half-
stretched first one drowsy pinion and then the other, that their
stomachs craved for food; whereupon, after much yawning and
stretching, they dragged themselves from their holes and went
prowling after bats and mice in the dark. Tired with their hunt, and
not over content with their supper, which was both coarse and
scanty, they thus laid their heads together, and, however dull by
nature, and doubly dulled by sleep, they were for once stimulated by
hunger and disappointment to something like ingenuity.
Said an old gray-headed owl: "This barbarous exercise ill suits with
my years and my gravity."
"And this barbarous fare," said a pert, idle youngster, "ill suits with
the youthful activity of my stomach."
"I'll stake my reputation upon it," said a third, shaking his dull
head, "but that proud, self-sufficient gormandizing eagle has eaten a
whole sheep for his supper."
"And I'll stake mine," yawned a fourth, "that his first cousin, the
vulture, and his second cousin, the hawk, have feasted; the one on
a fat lamb, and the other on a hen and chickens."
"Chut," said the first old grey-beard, "we'll feast ere long on
sheep, lamb, hen, chickens, and all; ay! mayhap on the eagle's own
little ones, to say nothing of his cousins."
"How so," hooted out the whole junto—"you would not fight the
king of birds?"
"Let me alone for that; there are better weapons than beak or
talons; and so he and his subjects shall find. But you must all aid in
the enterprise."
"If there be no fighting, and not too much labour, and not too
much——"
"Peace! there shall be nothing but sleeping!"
"Sleeping?"
"Ay! and some talking. But leave that to me."
Here all the heavy heads poked forward, closing in a circle round
their Nestor; while all their great round eyes opened in full stare
upon his.
"To-morrow you must all sleep as usual, until I give a long hoot;
then you must all open your eyes and observe what shall chance."
Tired with so unusual a debate, all went to sleep accordingly, and
snored louder than usual; until, just as the sun had awakened to full
life and stir all the feathered tribe, the old owl hooted and screeched
forth such a yell, as first terrified and then attracted on wings,
spurred by curiosity, though still trembling with fear, every bird of
the air from the giant eagle to the diminutive wren.
"A vision! a vision!" cried the owl; and again he screeched and
again he hooted, rustling up all his feathers, flapping his wings,
blinking his eyes, and tumbling head over tail like a bird distracted.
Every creature present stared and wondered.
"A vision, a vision! A miracle, a miracle!" again shouted the owl.
"I have seen a bird larger than the ostrich and stronger than the
eagle. Lightnings flame from his eyes, and thunder roars from his
beak. He has spoken; and lo! his command was: The owls are my
servants and to them I make known my will. Let all the birds of the
air hearken to their voice. Let them do their bidding, respect their
repose, and feed them with the fat of the land; or, behold, I will feed
upon them."
Thereupon, the owls set up a hoot in chorus, and all the birds
scattered to the four winds to collect food for the servants to eat,
lest the unseen master should eat them.
From this time forth these stupid owls were deemed the wisest of
the birds of the air; they supped every night upon fat yearlings; and
when they hooted all the feathered tribe clapped their wings and
sang a song of praise.
BIRDS' NESTS

Ernest Ingersoll

A bird's nest is a bird's house. Sometimes it is strong, well made


and tightly roofed, and sometimes it is not, just as with men's
houses. The principal difference between the bird's house and ours
is that we build ours to be used all the year round, while the bird
prefers to make a new one each summer. There are some birds,
such as the fish-hawk, however, that keep the same nest many years
in succession, repairing it each spring; and I think more birds would
do so were it not that their houses are usually made so slightly that
the winter's gales knock them to pieces when the owners are absent
at some Southern health resort. This is a pity, too, for many of our
commonest nests are exceedingly pretty and call for a great deal of
work and care on the part of the builders, whose only tools are their
feet and beaks.
Take, for instance, the lovely hammock-like basket, hung by its rim
beneath the fork of some low branch, which is made by the little
grey, red-eyed vireo, which carols to us all the early summer days
from every garden and orchard. Such a nest was hung in a maple
close to my porch. The bird had built it within arm's length of where
we were constantly passing, yet we never saw it until it was quite
finished; and the only way we could get a look at it then was by
pulling aside a branch. This care was not taken from fear of us, but
in the hope that the cradle would escape the sharp eyes of red
squirrels, weasels, bluejays, and other creatures who hunt for and
rob birds' nests of eggs and young to get food for themselves. I am
happy to say, however, that the vireo's nest was not disturbed.
How to hide their nests safely is the great question in the minds of
all the little birds. The big, strong ones do not need to worry about
that so much, because they can drive away most robbers; therefore,
we find that the hawks and crows, jays, kingbirds, and others able to
take care of themselves, usually set their baskets in the crotch of
some tree, where they can be seen easily enough, but all nests of
this kind are strongly made, and fastened so that the winds shall not
rock them out of their places or spill the contents.
But the little birds try to hide their homes in various clever ways. A
good many seek holes and crannies. The woodpeckers are able to
dig these for themselves, for their beaks are like chisels. Others, like
the wrens, bluebirds, nuthatches, chicadees, and so forth, find knot-
holes, places where a branch has broken off, and various small
hollows, in which to make their beds, where the young will be snug
in bad weather, and pretty safe from all enemies except snakes.
Others, like the kingfisher, the sand-swallow, and certain sea-
birds, make or find holes in earth-banks and rocky cliffs, so that their
babies are born in a tiny cave. All of our swallows, before the
country was settled by white people, lived in this manner or in
hollow trees; but as soon as civilization came those we soon named
barn-swallows left the wilds and put their nests under the roofs of
barns and other outbuildings. Then some one, remembering the
ways of old England, began to put bird-houses in the gardens; and
now, in all parts of the United States, you may find those cousins of
the swallows, the purple martins, living by the dozen in these lofty
little hotels on the top of a pole.
The nests of the cliff-swallows are little jugs of mud, plastered by
their bases to the face of the rock. The birds make them by bringing
pellets of mud in their bills from some stream-side, and putting them
one upon another, until each pair has formed a windowless, bottle-
like house, with a front door like the neck of the jug, so small that
no big bird can enter it. These are very safe and snug nests, and the
birds can sit in their doorways and gossip with each other very
sociably, for the nests are crowded together like the houses in a city
block. This is the same kind of swallow that now puts its nest in
rows along the outside of our barns under the eaves; but often they
are mere cups instead of jugs, because the barn roof sheds the rain,
and a clay roof is no longer necessary to protect the feather bed
inside.
Another one of the small birds that is more and more coming to
seek our protection and sympathy is the greenish-brown flycatcher
that (as some folks think) calls out her own name every few
minutes, Phoebe, Phoebe. She makes her home very solidly of mud
and moss, lined with horse-hair, and in the old days always rested it
on a ledge of rock, as many still do. Most of the phoebes, however,
now think it easier and safer to get under a roof, and so they put
their mossy cups on the stone piers or supporting timbers of
bridges, among the rafters of sheds and porches, and in similar
places.
A great number and wide variety of birds make their houses upon
the ground. Most of the sea-birds do so—along the ledges of the
sea-cliff. Nearly all the water fowl and game birds (except herons)
also do so; and most of the ducks and similar birds nestle among the
wet reeds of marshes, where their rude bedding is damp all the time
and sometimes soaking wet. To keep their eggs warm when they
have to leave them for a time, many of the ducks pluck a large
quantity of downy feathers from their breasts with which to cover
the eggs. The eider of the arctic regions is the foremost in this
practice, and the eider-down sold in shops is gathered from their
nests; but it is a habit of many other ducks. One of the most
interesting of these ground-nest birds is the least bittern, a solitary
bird frequenting swamps and marshy places.
Not only the water-birds, however, but some of the smallest and
prettiest of our songsters choose to dwell and lay their eggs close to
the ground, although they seem to be exposed there to many more
dangers than are those in the treetops or elsewhere. None try more
anxiously to hide their homes than do these ground-nesters, arching
the grasses above them, or building little sheds of leaves to protect
and hide the shining eggs. (Adapted.)
HATTO THE HERMIT: THE
LEGEND OF A BIRD'S NEST

Selma Lagerlöf

Hatto, the hermit, stood in the desert and prayed to God. The
storm was on, and his long hair and beard blew about him as wind-
whipped grass blows about an old ruin. But he did not brush back
the hair from his eyes, nor did he fasten his long beard to his girdle,
for his arms were raised in prayer. Since sunrise he had held his
gaunt, hairy arms out-stretched toward heaven, as untiring as a tree
stretching out its boughs, and thus he would remain until evening. It
was a great thing for which he was praying.
He was a man who had suffered much from the wickedness and
dishonesty of the world. He himself had persecuted and tortured
others, and persecution and torture had been his portion, more than
he could endure. Therefore, he had gone forth into the wilderness,
had dug himself a cave on the river bank, and had become a holy
man whose prayers found hearing at the throne of God.
Hatto, the hermit, stood on the river bank before his cave and
prayed the great prayer of his life. He prayed God to send down the
Day of Judgment upon this wicked world. He cried to the angels of
the trumpets, who are to herald the end of the reign of sin.
Round about him was the wilderness, barren and desolate. But a
little up the bank stood an old willow with shortened trunk, which
swelled out at the top of a round hump like a queer head, and from
it new, freshly green twigs were sprouting. Every autumn the
peasants from the unwooded flatlands robbed the willow of her fresh
new shoots. But every year the tree put forth new ones, and on
stormy days the slender, flexible twigs whipped about the old willow,
as hair and beard whipped about Hatto, the hermit.
It was just on this day that a pair of water thrushes, who usually
built their nest on the trunk of the old willow between the new
twigs, had decided to begin their work. But the wild whipping of the
twigs disturbed the birds. They flew up with their bits of dry grass
with nothing accomplished. Then it was that they caught sight of old
Hatto.
No one now living can picture to himself how moss-grown and
dried-up, how gnarled and black and generally unlike a human
being, such an old desert hermit can become. His skin clung so close
to forehead and cheekbones that his head looked like a skull, and
only a tiny gleam down in the depth of his eyeballs showed that
there was still life in him. The dried-up muscles gave no curve to the
body; the outstretched naked arms were merely a couple of narrow
bones, covered with hard, wrinkled, bark-like skin. He wore an old
black cloak, clinging close to his body. He was tanned brown by the
sun and black with dirt. His hair and beard alone were of a lighter
shade, for rain and sunshine had faded them to the grey-green hue
of the under side of willow leaves.
The birds, flying about uneasily and seeking a place for their nest,
took Hatto the hermit to be another old willow cut off by axe and
saw in its heavenward striving. They flew about him many times,
flew away and returned again, took note of the guide posts on the
way to him, calculated his position in regard to protection from
storm and birds of prey, found it rather unfavourable, but decided to
locate there on account of the close vicinity of the stream and the
reeds, their chief source of supply. One of the birds shot down
suddenly and laid a bit of grass in the hermit's outstretched hand.
The storm had abated a little, so that the straw was not blown
from his hand at once, but the hermit did not pause in his prayer,
"Come soon, O Lord, come to destroy this world of sin, that mankind
may not more increase its load of guilt."
The storm roared out again, and the bit of grass fluttered out of
the hermit's great bony hand. But the birds came again and
endeavoured to erect the cornerstone of their new home between
his fingers. Suddenly a dirty, clumsy thumb laid itself over the grass
spears and held them in firm position, while four fingers reached
over the palm, making a peaceful niche where a nest would be safe.
The hermit continued his untiring supplications, and before his eyes
danced fever visions of the day of judgment. The earth trembled,
the skies shot fire. He saw the black clouds of hurrying birds
beneath the glowing firmament; herds of fleeing animals spread
over the earth. But while his soul was filled with these visions of
fever, his eyes began to watch the flight of the tiny birds that came
and went with lightning dashes, laying new straws in the nest with
little chirps of pleasure.
The old man did not move. He had made a vow to stand the
entire day with outstretched arms, in order to force God to hear him.
The little thrushes built and built busily all the day, and their work
progressed finely. There was no lack of material in this wilderness of
rolling ground with stiff grass and brush, and on the river bank, with
its reeds and rushes. They could not take time for dinner or supper.
They flew back and forth, glowing with interest and pleasure, and
when dusk came they had reached the peak of their roof.
But before evening fell the hermit's eyes had come to rest on their
labour more and more. He watched them in their flight; he scolded
them when they were clumsy; he grieved when the wind spoiled
their efforts, and he became almost angry when they stopped a
moment to rest.
Then the sun sank and the birds sought their accustomed resting
place among the reeds, safe from all harm, for no enemy could
approach without a warning splash of the water or a quivering of the
reeds.
When the morning broke, the thrushes thought at first that the
events of the preceding day had been but a beautiful dream.
They found their guideposts and flew straight to their nest, but
the nest had disappeared. They peered out over the moors and flew
high up to gain a wider view. But there was no sign of nest or tree.
Finally they sat down on a stone by the water and thought the
matter over. They wagged their tails and turned their heads to right
and left. Where were nest and tree?
But scarcely had the sun raised itself a hand's breadth over the
belt of woods beyond the stream, when their tree suddenly came
wandering up and stood itself upon the selfsame place it had
occupied the day before. It was as black and as gnarled as before,
and it carried their nest on the tip of something that was probably a
thin, upright bough.
The birds began to build again without attempting to ponder
further over the many miracles of nature.
Hatto, the hermit, who chased the little children from his cave and
told them it were better for them if they had never seen the light of
day; he who waded out deep into the mud of the river to hurl curses
after the flagged boats filled with gay young people rowing past; he
from whose evil glance the shepherds carefully guarded their flocks,
he did not return to his place on the river bank because of thought
for the little birds. But he knew that not only every letter in the Holy
Book has its own mystical meaning, but that everything that God
allows to happen in the natural world has its significance also. And
he had discovered what it might mean, this sign of the birds building
in his hand: God had willed that he should stand with outstretched
arm until the birds had raised their young—could he do this, then
would his prayer be heard.
But on this day his glance followed the motions of the birds with
greater attention. He saw the rapid completion of the nest. The tiny
builders flew around it and examined it carefully. They brought a few
rags of moss from the real willow and plastered them on the outside
as a finishing decoration. They brought the softest young grass, and
the female bird pulled the down from her breast to furnish the
inside.
The peasants of the neighbourhood, who feared the evil power
which the prayers of the hermit might have with God, were used to
bring him bread and milk to soften his anger. They came now, and
found him standing motionless, the bird's nest in his hand.
"See how the holy man loves the little creatures," they said, and
feared him no longer. They raised the milk can to his lips and fed
him with the bread. When he had eaten and drunk he drove them
away with curses, but they smiled at his anger.
His body had long since become the servant of his will. He had
taught it obedience by hunger and scourge, by days of kneeling and
sleepless nights. Now his muscles of steel held his arm outstretched
days and weeks, and while the mother bird sat on her eggs and did
not leave the nest, he would not go to his cave even to sleep at
night. He learned how to sleep standing with outstretched arm.
He grew accustomed to the two uneasy little eyes that peered
down at him over the edge of the nest. He watched for rain and hail,
and protected the nest as well as he could.
One day the little mother left her place. Both thrushes sat on the
edge of the nest, their tails moving rapidly, holding great
consultation and looking very happy, although the whole nest
seemed filled with a frightened squeaking. After a little they set out
upon an energetic gnat hunt.
One gnat after another fell before them, and was brought home to
that which squeaked and peeped up there in his hand. And the
peeping grew more intense whenever the food was brought in. It
disturbed the holy man at his prayers. Gently, very gently, his arm
sank down on the joints that had almost lost the power of motion,
until his deep-set, glowing eyes peered into the nest.
Never had he seen anything so ugly and so miserable—naked little
bodies, with a few scattered down tufts, no eyes, no strength to fly,
nothing but six great open beaks.
He could not understand it himself, but he liked them just as they
were. He had not thought to make an exception of the old birds in
his prayers for the great Doom, but when he now implored God to
release the world through utter destruction, he made a silent
exception in favour of these six little helpless creatures.
When the peasant women brought him food he no longer
rewarded them with curses. As he was necessary for the little ones
up there in his hand, he was glad that the people did not let him
starve.
Soon six little round heads peered all day over the edge of the
nest. Old Hatto's arm sank to the level of his eyes more and more
frequently. He saw the feathers grow out of the red skin; he saw the
eyes open and the little bodies round out. The fortunate inheritance
of all the beauty with which nature endows the feathered denizens
of the air came early into their heritage.

And, meanwhile, the prayers for the great destruction came more
and more slowly from Hatto's lips.
He believed he had God's promise that it should come as soon as
the little birds were able to fly. And now he stood there seeking an
escape from God. For he could not sacrifice these six little ones,
whom he had watched and cared for.
It had been different before, when he had had nothing of his own
to care for. Love of the small and the helpless—that love which every
little child must teach to the dangerous grown man—this love came
over him and made him hesitate.
Sometimes he wished that he could throw the entire nest into the
stream, for he still believed that those alone are to be envied who
die without having known care or sin. Was it not his duty to save
these little ones from beasts of prey, from cold and hunger and all of
the many ills of life? But just as he was pondering on this, a hawk
swooped down on the nest to kill the little ones. Hatto caught the
robber in his left hand, whirled him around his head, and threw him
far out into the stream.
Then came the day when the little ones were ready to fly. One of
the old birds sat inside the nest, trying to push the young ones out
on the edge, while the other flew about and showed them how easy
it was if they would only try. But as the young ones would not
overcome their fear, both old birds flew out before them, showing off
all their prettiest arts and tricks. They turned and twisted in the air,
they shot up straight as does the lark, or they hung motionless on
rapidly fluttering wings.
But the little ones would not move, and then Hatto decided to
interfere in the matter himself. He gave them a careful push with
one finger, and thus ended the dispute. They tumble out, trembling
and uncertain, hitting at the air as bats do; they sink down, but rise
up again; they find the proper motion and use it at once to regain
the nest. The old birds come back to them in happy pride, and Hatto
chuckles.
It was he who had brought the matter to such a happy conclusion.
And now he pondered most seriously the question as to whether a
loophole of escape could be found for God.
Perhaps, when one comes to think of it, God holds this earth like a
bird's nest in His right hand and perhaps He loves those within it—all
the helpless children of earth. Perhaps He is merciful to them whom
He had vowed to destroy, just as the hermit was merciful to the little
birds. Of course the hermit's birds were much better than God's
human beings, but he could still understand that God might have
pity for them in His heart.
Next day the nest was empty, and the bitterness of loneliness
came over the hermit. His arm sank slowly down at his side, and it
seemed to him that all nature held its breath to hear the roar of the
trumpets announcing the Last Judgment. But in the same moment
all the birds returned and settled down on his head and shoulders,
for they had no fear of him. And a light shot through the tortured
brain of the old hermit. He had lowered his arm every day to look at
the birds.
And then, as he stood there, the six young birds flying about him,
he nodded, smiling, to some one whom he could not see.
"Thou art free," he said. "Thou art free. I did not keep my vow,
therefore Thou needst not keep Thine."
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

ebookbell.com

You might also like