159
Chapter 6
Using Decision Structures
After completing this chapter, you will be able to:
n Write conditional expressions.
n Use an If... Then statement to branch to a set of program statements based on
a varying condition.
n Use the MaskedTextBox control to receive user input in a specific format.
n Short-circuit an If... Then statement.
n Use a Select Case statement to select one choice from many options in program code.
n Use the Name property to rename objects within a program.
n Manage mouse events and write a MouseHover event handler.
In the past few chapters, you used several features of Microsoft Visual Basic 2010 to process
user input. You used menus, toolbars, dialog boxes, and other Toolbox controls to display
choices for the user, and you processed input by using property settings, variables, operators,
formulas, and the Microsoft .NET Framework.
In this chapter, you’ll learn how to branch conditionally to a specific area in your program
based on input you receive from the user. You’ll also learn how to evaluate one or more
properties or variables by using conditional expressions, and then execute one or more
program statements based on the results. In short, you’ll increase your programming
vocabulary by creating code blocks called decision structures that control how your program
executes, or flows, internally.
Event-Driven Programming
The programs you’ve written so far in this book have displayed Toolbox controls, menus,
toolbars, and dialog boxes on the screen, and with these programs, users could manipulate
the screen elements in whatever order they saw fit. The programs put the user in charge,
waited patiently for a response, and then processed the input predictably. In programming
circles, this methodology is known as event-driven programming. You build a program by
creating a group of “intelligent” objects that know how to respond to input, and then the
program processes the input by using event procedures associated with the objects.
Where does this input come from? Fundamentally, of course, most input comes from the user
of your program, who is opening menus, clicking the mouse, typing in text boxes, and so on.
However, program input can also come from the computer system itself. For example, your
program might be notified when a piece of e-mail arrives or when a specified period of time
Table of Contents
Using Decision Structures.................................. 159
Event-Driven Programming.......................................... 159
Using Conditional Expressions........................................ 161
If... Then Decision Structures................................. 161
Testing Several Conditions in an If... Then
Decision Structure............................................. 162
Using Logical Operators in Conditional Expressions................ 167
Short-Circuiting by Using AndAlso and OrElse................... 169
Select Case Decision Structures...................................... 171
Using Comparison Operators with a Select
Case Structure................................................. 173
One Step Further: Detecting Mouse Events............................ 177
Chapter 6 Quick Reference........................................... 179