Visual Basic 2013-0048
Visual Basic 2013-0048
The SpinButton_Click event handler is executed when the user clicks the Spin button on the page.
Essentially, the event handler performs four main tasks:
3. It creates three random numbers and displays them in text block objects.
4. It displays the Coins.jpg photo and plays a sound when the number 7 appears.
You've probably declared and used variables before in programs. But notice the variable type
here—the generator is declared using the type Random, which has been specifically designed to
support the creation of so-called "pseudo-random" numbers—that is, numbers that don't follow a
particular pattern and appear in a specific range. You'll use random numbers often in this book, and
you'll learn much more about data types and conversion in Chapter 11, "Mastering data types, opera-
tors, and string processing."
CoinImage.Visibility = Windows.UI.Xaml.Visibility.Collapsed
As you learned earlier, the Visibility property determines whether or not an object on a page is vis-
ible. This specific syntax uses the objects in the .NET Framework to collapse (or hide) the photo of the
coins. (This line is designed to restore the program to a neutral state if a previous spin had displayed
the coins.)
The next three lines handle the random number computations. Does this concept sound strange?
You can actually make Visual Basic generate unpredictable numbers within specific guidelines—that
is, you can create random numbers for lottery contests, dice games, or other statistical patterns. The
generator instance's Next method in each line creates a random number between 0 and 9—just what
you need for this particular slot machine application.
FirstNum.Text = generator.Next(0, 9)
SecondNum.Text = generator.Next(0, 9)
ThirdNum.Text = generator.Next(0, 9)
The last group of statements in the program checks whether any of the random numbers is 7. If
one or more of them is, the program displays the graphical depiction of a payout and plays the sound
effect to announce the winnings.
Each time the user clicks the Spin button, the SpinButton_Click event handler is executed, or called,
and the program statements in the handler are run again. However, if you click the Spin button many
times in rapid succession, you might miss one or more of the sound effects, because the media ele-
ment object can play only one sound effect at a time.
■■ Click the Start Debugging button on the Standard toolbar. (You'll typically see "Local Machine"
next to this button, because you debug on the local computer by default.)
■■ Press F5.
Try running your Lucky Seven program now. If Visual Basic displays an error message, you might
have a typing mistake or two in your program code. Try to fix it by comparing the printed version in
this book with the one you typed, or load Lucky Seven from your hard disk and run it.
Note I assume that you have named your project My Lucky Seven, but the instructions and
screen shots below will show Lucky Seven because you might be running the sample proj-
ect that I created.
The Lucky Seven program compiles and runs. After a few seconds, the user interface appears,
just as you designed it.
The program picks three random numbers and displays them in the labels on the page. When
a 7 appears, your screen will look like this: