0% found this document useful (0 votes)
30 views

Visual Basic 2013-0048

The SpinButton_Click event handler performs four main tasks when a user clicks the spin button: 1. It declares a random number generator to generate random numbers. 2. It hides the digital photo. 3. It generates three random numbers and displays them in text blocks. 4. It displays the photo and plays a sound if any number is 7, simulating a winning spin.

Uploaded by

almas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Visual Basic 2013-0048

The SpinButton_Click event handler performs four main tasks when a user clicks the spin button: 1. It declares a random number generator to generate random numbers. 2. It hides the digital photo. 3. It generates three random numbers and displays them in text blocks. 4. It displays the photo and plays a sound if any number is 7, simulating a winning spin.

Uploaded by

almas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

A look at the SpinButton_Click event handler

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:

1. It declares a random number generator named generator in the program.

2. It hides the digital photo.

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.

Let's look at each of these steps individually.

The random number generator is declared by this line of code:

Dim generator As New Random

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."

Hiding the photo is accomplished by the following line:

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.

Chapter 3  Creating your first Windows Store application   67


If (FirstNum.Text = "7") Or (SecondNum.Text = "7") Or
(ThirdNum.Text = "7") Then
CoinImage.Visibility = Windows.UI.Xaml.Visibility.Visible
CoinSound.Play()
End If

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.

Running Windows Store apps


Congratulations! You're ready to run your first Windows Store app. To run a Visual Basic program
from the IDE, you can do any of the following:

■■ Click Start Debugging on the Debug menu.

■■ 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.

Run the Lucky Seven program

1. Click the Start Debugging button on the Standard toolbar.

The Lucky Seven program compiles and runs. After a few seconds, the user interface appears,
just as you designed it.

2. Click the Spin button.

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:

68  PART I  Introduction to Visual Studio Development

You might also like