Clearly Visual Basic: Programming with Microsoft Visual Basic 2010, Second Edition Diane Zak - eBook PDF pdf download
Clearly Visual Basic: Programming with Microsoft Visual Basic 2010, Second Edition Diane Zak - eBook PDF pdf download
https://ebookluna.com/download/clearly-visual-basic-programming-
with-microsoft-visual-basic-2010-second-edition-ebook-pdf/
https://ebookluna.com/download/programming-with-microsoft-visual-
basic-2017-ebook-pdf/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-introduction-to-programming-
using-visual-basic-10th-edition/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-starting-out-with-visual-
basic-8th-edition-by-tony-gaddis/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-starting-out-with-visual-
basic-7th-edition-by-tony-gaddis/
ebookluna.com
Microsoft Visual Basic 2017 for Windows, Web, and Database
Applications: Comprehensive 1st Edition Corinne Hoisington
- eBook PDF
https://ebookluna.com/download/microsoft-visual-basic-2017-for-
windows-web-and-database-applications-comprehensive-ebook-pdf/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-microsoft-visual-c-an-
introduction-to-object-oriented-programming-7th-edition/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-starting-out-with-
visual-c-5th-edition/
ebookluna.com
https://ebookluna.com/download/microsoft-visual-c-step-by-step-9th-
edition-ebook-pdf/
ebookluna.com
https://ebookluna.com/download/basic-japanese-second-edition-ebook-
pdf/
ebookluna.com
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.
CLEARLY VISUAL BASIC®
P R O G R A M M I N G W I T H M I C R O S O F T ® V I S U A L B A S I C ® 2 01 0
This page intentionally left blank
SECOND EDITION
DIANE ZAK
Australia l Brazil l Japan l Korea l Mexico l Singapore l Spain l United Kingdom l United States
Clearly Visual Basic: ª 2012 Course Technology, Cengage Learning
Programming with Microsoft
ALL RIGHTS RESERVED. No part of this work covered by the copyright herein may be
Visual Basic 2010, Second Edition reproduced, transmitted, stored or used in any form or by any means graphic, electronic,
Diane Zak or mechanical, including but not limited to photocopying, recording, scanning, digitizing,
taping, Web distribution, information networks, or information storage and retrieval
Executive Editor: Marie Lee
systems, except as permitted under Section 107 or 108 of the 1976 United States
Acquisitions Editor: Brandi Shailer Copyright Act, without the prior written permission of the publisher.
Senior Product Manager: Alyssa Pratt
Freelance Product Manager: Tricia Coia
For product information and technology assistance, contact us at
Associate Product Manager: Cengage Learning Customer & Sales Support, 1-800-354-9706
Stephanie Lorenz
For permission to use material from this text or product,
Marketing Manager: Shanna Shelton
submit all requests online at cengage.com/permissions
Senior Content Project Manager: Jill Braiewa Further permissions questions can be emailed to
Quality Assurance: Green Pen QA [email protected]
Art Director: Faith Brosnan
Cover Designer: Cabbage Design Company Library of Congress Control Number: 2011921829
Text Designer: Shawn Girsberger
ISBN 13: 978 1 111 53015 0
Print Buyer: Julio Esperas
ISBN 10: 1 111 53015 7
Proofreader: Suzanne Huizenga
Indexer: Alexandra Nickerson Course Technology
Compositor: Integra Software Services 20 Channel Center Street
Boston, MA 02210
USA
Some of the product names and company names used in this book have been used for
identification purposes only and may be trademarks or registered trademarks of their
respective manufacturers and sellers.
Course Technology, a part of Cengage Learning, reserves the right to revise this
publication and make changes from time to time in its content without notice.
Example: Microsoft® is a registered trademark of the Microsoft Corporation.
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: www.cengage.com/global
Cengage Learning products are represented in Canada by Nelson Education, Ltd.
To learn more about Course Technology, visit
www.cengage.com/coursetechnology
To learn more about Cengage Learning, visit www.cengage.com
Purchase any of our products at your local college store or at our preferred online store
www.cengagebrain.com
Brief Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
C H AP T E R 1 I Am Not a Control Freak! (Control Structures) . . . . . . . . . .1
C H AP T E R 2 First You Need to Plan the Party (Problem-Solving Process) . . . 11
C H AP T E R 3 I Need a Tour Guide (Introduction to Visual Basic 2010) . . . . . 27
C H AP T E R 4 Do It Yourself Designing (Designing Interfaces) . . . . . . . . . 49
C H AP T E R 5 The Secret Code (Assignment Statements) . . . . . . . . . . . 67
C H AP T E R 6 Where Can I Store This? (Variables and Constants) . . . . . . . 81
C H AP T E R 7 What’s Wrong with It? (Syntax and Logic Errors) . . . . . . . . .101
C H AP T E R 8 Decisions, Decisions, Decisions (Selection Structure) . . . . . .115
C H AP T E R 9 Time to Leave the Nest (Nested Selection Structures) . . . . . .139
C H AP T E R 1 0 So Many Paths . . . So Little Time (Multiple-Alternative
Selection Structures) . . . . . . . . . . . . . . . . . . . .159
C H AP T E R 1 1 Testing, Testing . . . 1, 2, 3 (Selecting Test Data) . . . . . . . .183
C H AP T E R 1 2 How Long Can This Go On? (Pretest Loops) . . . . . . . . . . .203
C H AP T E R 1 3 Do It, Then Ask Permission (Posttest Loops) . . . . . . . . . .231
C H AP T E R 1 4 Let Me Count the Ways (Counter-Controlled Loops) . . . . . . .241
C H AP T E R 1 5 I’m on the Inside; You’re on the Outside (Nested Loops) . . . . .261
C H AP T E R 1 6 I Hear You Are Breaking Up (Sub Procedures) . . . . . . . . . .279
C H AP T E R 1 7 Talk to Me (Function Procedures) . . . . . . . . . . . . . . .303
C H AP T E R 1 8 A Ray of Sunshine (One-Dimensional Arrays) . . . . . . . . . .319
C H AP T E R 1 9 Parallel and Dynamic Universes (More on One-Dimensional
Arrays) . . . . . . . . . . . . . . . . . . . . . . . . . . .341
C H AP T E R 2 0 Table Tennis, Anyone? (Two-Dimensional Arrays) . . . . . . . .363
C H AP T E R 2 1 Building Your Own Structure (Structures) . . . . . . . . . . . .383
vi
CONTENTS
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Preface
Clearly Visual Basic: Programming with Microsoft Visual Basic 2010, Second Edition is designed
for a beginning programming course. The book assumes students have no previous
programming knowledge or experience. However, students should be familiar with basic
Windows skills and file management. The book’s primary focus is on teaching programming
concepts, with a secondary focus on teaching the Visual Basic programming language. In other
words, the purpose of the book is to teach students how to solve a problem that requires a
computer solution. The Visual Basic language is used as a means of verifying that the solution
works correctly.
Approach
Rather than focusing on a specific programming language, Clearly Visual Basic:
Programming with Microsoft Visual Basic 2010, Second Edition focuses on programming
concepts that are common to all programming languages—such as input, output, selection,
and repetition. Concepts are introduced, illustrated, and reinforced using simple examples
and applications, which are more appropriate for a first course in programming. The
concepts are spread over many short chapters, allowing students to master the material one
small piece at a time. Because its emphasis is on teaching the fundamentals of programming,
the book covers only the basic controls, properties, and events available in Visual Basic.
Each chapter provides the steps for creating and/or coding an application that uses the
concepts covered in the chapter. The videos and PDF files that accompany each chapter are
designed to help students master the chapter’s concepts.
xv
Organization and Coverage
Features
Clearly Visual Basic: Programming with Microsoft Visual Basic 2010, Second Edition is an
exceptional textbook because it also includes the following features:
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.
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, provide additional
information about the concepts, or cover topics related to the concepts. The videos are available
online at www.cengagebrain.com. Search for the ISBN of your title (from the back cover of your
book) using the search box at the top of the page. This will take you to the product page where
free companion resources can be found.
WANT MORE INFO? FILES These notes direct students to files that accompany each chapter in
the book. The files contain additional examples and further explanations of the concepts
covered in the chapter. The files are in PDF format. The files are available online at www.
cengagebrain.com.
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.
OBJECTIVES Each chapter begins with a list of objectives so you know the topics that will be
presented in the chapter. In addition to providing a quick reference to topics covered, this
feature provides a useful study aid.
MINI-QUIZZES Mini-Quizzes are strategically placed to test students’ knowledge at various
points in each chapter. Answers to the quiz questions are provided in Appendix B in the book.
SUMMARY Each chapter contains a Summary section that recaps the concepts covered in the
chapter.
KEY TERMS Following the Summary section in each chapter is a listing of the key terms
introduced throughout the chapter, along with their definitions.
REVIEW QUESTIONS Each chapter contains Review Questions designed to test a student’s
understanding of the chapter’s concepts.
EXERCISES The Review Questions in each chapter are followed by Exercises, which provide
students with additional practice of the skills and concepts they learned in the chapter. The
Exercises are designated as TRY THIS, MODIFY THIS, INTRODUCTORY, INTERMEDIATE,
ADVANCED, FIGURE THIS OUT, and SWAT THE BUGS.
TRY THIS EXERCISES The TRY THIS Exercises should be the first Exercises students complete
after reading a chapter. These Exercises are similar to the application developed in the chapter,
and they allow students to test their understanding of the chapter’s concepts. The answers to
TRY THIS Exercises are provided in Appendix B in the book.
MODIFY THIS EXERCISES In these Exercises, students modify an existing application.
FIGURE THIS OUT EXERCISES These Exercises require students to analyze a block of code and
then answer questions about the code.
SWAT THE BUGS EXERCISES The SWAT THE BUGS Exercises provide an opportunity for
students to detect and correct errors in an existing application.
xvi
PREFACE Instructor Resources and Supplements
WEB APPLICATIONS The Web Application chapter (Chapter 27) is now in the book rather than
online. The chapter now shows students how to create a Web application that contains two
Web pages. It also covers the LinkButton tool.
REVIEW QUESTIONS AND EXERCISES Additional Review Questions and Exercises have been
added to each chapter.
APPENDIX B Appendix B contains the answers to each chapter’s Mini-Quizzes and TRY THIS
Exercises. The answers are provided to give students immediate feedback and more opportunity
for learning.
POSTTEST LOOPS AND STRING CONCATENATION In the previous edition of the book, posttest
loops and string concatenation were covered in the same chapter. In this edition, posttest loops
are covered in a separate chapter (Chapter 13). String concatenation is now covered along with
counter-controlled loops in Chapter 14.
ARITHMETIC ASSIGNMENT OPERATORS These operators are covered along with pretest loops
in Chapter 12.
SELECTION STRUCTURES The selection structure chapters (Chapters 8, 9, and 10) now refer to
the different forms of the selection structure as single-alternative, dual-alternative, and multiple-
alternative.
REPETITION STRUCTURES The repetition structure chapters (Chapters 12, 13, and 14) were
revised to include the following terms: looping condition and loop exit condition.
STRING MANIPULATION The Remove method was added to the string manipulation chapter
(Chapter 23).
AUTO-IMPLEMENTED PROPERTIES Auto-implemented properties are covered in the video that
accompanies the chapter on classes (Chapter 26).
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.
DATA FILES Data Files are necessary for completing the computer activities in this book. The
Data Files are provided on the Instructor Resources CD-ROM and also may be found at www.
cengagebrain.com.
SOLUTION FILES Solutions to the chapter applications and the end-of-chapter Review
Questions and Exercises are provided on the Instructor Resources CD-ROM and also may be
found at www.cengagebrain.com. The solutions are password protected.
DISTANCE LEARNING Course Technology is proud to present online test banks in WebCT,
Blackboard, and Angel to provide the most complete and dynamic learning experience possible.
Instructors are encouraged to make the most of the course, both online and offline. For more
information on how to access the online test bank, contact your local Course Technology sales
representative.
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 Jill Braiewa (Senior Content Project Manager), Alyssa
Pratt (Senior Product Manager), Tricia Coia (Freelance Product Manager), Nicole Ashton
(Quality Assurance), Suzanne Huizenga (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: Wayne Payton,
Gadsden State Community College; Annette Kerwin, College of DuPage, Matthew Alimagham,
Spartanburg Community College; Anthony Basilico, College of Rhode Island; Frank Malinowski,
Darton College; Craig Brown, Boston College; and Laura Gipson, Beaufort Community College.
Diane Zak
xviii
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 ClearlyVB2010 folder. The files for Chapter 3 are stored in the ClearlyVB2010\Chap03
folder. Similarly, the files for Chapter 4 are stored in the ClearlyVB2010\Chap04 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 chapter
applications and Exercises in this book.
3. Click Tools on the menu bar and then click Options to open the Options dialog box. If
necessary, deselect the Show all settings check box. Click the Projects and Solutions
node. Use the information shown in Figure 3-3 in Chapter 3 to select and deselect the
appropriate check boxes. (Your dialog box will look slightly different if you are using
Visual Basic 2010 Express.).
4. Expand the Projects and Solutions node in the Options dialog box and then click VB
Defaults. Verify that both Option Explicit and Option Infer are set to On. Also verify that
Option Strict and Option Compare are set to Off and Binary, respectively. Click the OK
button to close the Options dialog box.
Figures
The figures in this book reflect how your screen will look if you are using Microsoft Visual
Studio 2010 Professional Edition and a Microsoft Windows 7 system. Your screen may appear
slightly different in some instances if you are using another version of Microsoft Visual Studio,
Microsoft Visual Basic, or Microsoft Windows.
To the Instructor
To complete the computer activities in this book, your students must use a set of data files. The
files are included on the Instructor’s Resource CD. They also may be obtained electronically at
www.cengagebrain.com.
The material in this book was written using Microsoft Visual Studio 2010 Professional Edition
on a Microsoft Windows 7 system. It was Quality Assurance tested using the Express Editions of
Microsoft Visual Basic 2010 and Microsoft Visual Web Developer 2010 on a Microsoft
Windows 7 system. The book assumes that both Option Explicit and Option Infer are set to On,
Option Strict is set to Off, and Option Compare is set to Binary. To verify these settings, start
either Visual Studio 2010 or Visual Basic 2010 Express. Click Tools on the menu bar and then
click Options. Expand the Projects and Solutions node in the Options dialog box and then click
VB Defaults. Verify the four Option settings and then click the OK button to close the Options
dialog box.
Control Structures
All computer programs, no matter how simple or how complex, are written using one or more of
three basic structures: sequence, selection, and repetition. These structures are called control
structures or logic structures, because they control the flow of a program’s logic. You will use the
sequence structure in every program you write. In most programs, you also will use both the
selection and repetition structures. This chapter gives you an introduction to the three control
structures. It also introduces you to a computerized mechanical man named Rob, who will help
illustrate the control structures. More detailed information about each structure, as well as how to
implement these structures using the Visual Basic language, is provided in subsequent chapters.
1. walk forward
2. walk forward 2 steps
algorithm 3. turn right 90 degrees
4. turn right 90 degrees
5. sit down
every time you drive your car and approach a railroad crossing. Your decision, as well as the
appropriate action, is based on whether the crossing signals (flashing lights and ringing bells) are
on or off. If the crossing signals are on, you stop your car before crossing the railroad tracks;
otherwise, you proceed with caution over the railroad tracks. When used in a computer
program, the selection structure alerts the computer that a decision needs to be made, and it
provides the appropriate action to take based on the result of that decision.
You can observe how the selection structure works by programming Rob, the mechanical man.
In this example, Rob is holding either a red or yellow balloon, and he is facing two boxes. One of
the boxes is colored yellow and the other is colored red. The two boxes are located three steps
away from Rob. Your task is to have Rob drop the balloon into the appropriate box: the yellow
balloon belongs in the yellow box, and the red balloon belongs in the red box. To write an
algorithm to accomplish the current task, you need to use four additional instructions from
Rob’s instruction set: if the balloon is red, do this:, otherwise, do this:, drop the balloon in the red
box, and drop the balloon in the yellow box. The additional instructions allow Rob to make a
decision about the color of the balloon he is holding, and then take the appropriate action based
on that decision. Figure 1-2 shows an illustration of the current example, along with the correct
algorithm. Notice that the drop the balloon in the red box and drop the balloon in the yellow box
instructions are indented within the selection structure. Indenting in this manner clearly
indicates the instructions to be followed when the balloon is red, as well as the ones to be
followed when the balloon is not red.
1. walk forward
2. walk forward
3. walk forward
4. if the balloon is red, do this:
drop the balloon in the red box
indent otherwise, do this:
Red
drop the balloon in the yellow box 3 steps
Yellow
July 27.
St. Pantaleon, A. D. 303. Sts. Maximian, Malchus,
Martinian, Dionysius, John, Serapion, and
Constantine, the Seven Sleepers, A. D. 250. St.
Congail, St. Luica.
THE SEVEN SLEEPERS.
FLORAL DIRECTORY.
Purple Loosestrife. Lythrum Salicaria.
Dedicated to St. Pantaleon.
July 28.
Sts. Nazarius and Celsus, A. D. 68. St. Victor, Pope, A. D.
201. St. Innocent I. Pope, A. D. 417. St. Sampson, A. D.
564.
Musical Prodigies.
FLORAL DIRECTORY.
July 29.
St. Martha V. Sts. Simplicius and Faustinus, brothers,
and Beatrice, their sister, A. D. 303. St. William, Bp. A.
D. 1234. St. Olaus, or Olave, king of Norway, A. D.
1030. St. Olaus, king of Sweden.
WATER IN WARM WEATHER.
FLORAL DIRECTORY.
July 30.
Sts. Abden and Sennen, A. D. 250. St. Julitta, A. D.
303.
Witchcraft.
The reporter of the trial states, from the mouth of John Osborne,
the following particulars not deposed to in court, namely: that as
soon as the mob entered the vestry-room, they seized him and his
wife, and Red Beard carried her across his shoulders, like a calf,
upwards of two miles, to a place called Gubblecut; where not finding
a pond they thought convenient, they then carried them to Marlston-
green, and put them into separate rooms in a house there; that they
there stripped him naked, and crossed his legs and arms, and bent
his body so, that his right thumb came down to his right great toe,
and his left thumb to his left great toe, and then tied each thumb
and great toe together; that after they had so done, they got a cloth,
or an old sheet, and wrapped round him, and then carried him to the
Mere on the green, where he underwent the discipline as has been
related in the course of the trial. What they did with his wife he
could not say, but he supposed they had stripped her, and tied her in
the same manner as himself, as she appeared naked in the pond
when the sheet was drawn from off her, and her thumbs and toes
tied as his were. After the mob found the woman was dead, they
carried him to a house, and put him into a bed, and laid his dead
wife by his side; all which he said he was insensible of, having been
so ill-used in the pond, as not to have any sense of the world for
some time; but that he was well assured it was so, a number of
people since informing him of it who were present. His wife, if she
had lived till Michaelmas, would have been seventy years of age; he
himself was but fifty-six.
The infatuation of the people in those parts of Hertfordshire was
so great, in thinking that these people were a witch and a vizard,
that when any cattle died, it was always said that Osborne and his
deceased wife had bewitched them. And even after the trial, a great
number of people in that part of the country thought the man a
vizard, and that he could cast up pins as fast as he pleased. Though
a stout able man of his age, and ready and willing to work, yet none
of the farmers thereabouts would employ him, ridiculously believing
him to be a vizard, so that the parish of Tring were obliged to
support him in their workhouse after his wife’s death.
So far is reported by the editor of the trial.
FLORAL DIRECTORY.
July 31.
St. Ignatius, of Loyola, A. D. 1556. St. John Columbini, A.
D. 1367. St. Helen, of Sweden, A. D. 1160.