Running Visual Basic Applications
Congratulations! You're ready to run your first real program. To run a Visual Basic program from the development environment, you can do any of the following:
Click Start Debugging on the Debug menu.
Click the Start Debugging button on the Standard toolbar.
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 Lucky7 from your hard disk and run it.
Run the Lucky Seven program
Click the Start Debugging button on the Standard toolbar.
The Lucky Seven program compiles and runs in the IDE. After a few seconds, the user interface appears, just as you designed it.
Click the Spin button.
The program picks three random numbers and displays them in the labels on the form, as follows:
Because a 7 appears in the first label box, the digital photo depicting the payoff appears, and the computer beeps. You win! (The sound you hear depends on your Sounds And Multimedia setting (or the Sounds And Devices setting) in Windows Control Panel. To make this game sound really cool, change the Default Beep sound to something more dynamic.)
Click the Spin button 15 or 16 more times, watching the results of the spins in the number boxes.
About half the time you spin, you hit the jackpot—pretty easy odds. (The actual odds are about 2.8 times out of 10; you're just lucky at first.) Later on you might want to make the game tougher by displaying the photo only when two or three 7s appear, or by creating a running total of winnings.
When you've finished experimenting with your new creation, click the End button.
The program stops, and the development environment reappears on your screen.
TIP
If you run this program again, you might notice that Lucky Seven displays exactly the same sequence of random numbers. There is nothing wrong here—the Visual Basic Rnd function was designed to display a repeating sequence of numbers at first so that you can properly test your code using output that can be reproduced again and again. To create truly “random” numbers, use the Randomize function in your code, as shown in the exercise at the end of this chapter. The .NET Framework, which you'll learn to use later, also supplies random number functions.
Sample Projects on Disk
If you didn't build the MyLucky7 project from scratch (or if you did build the project and want to compare what you created to what I built for you as I wrote the chapter), take a moment to open and run the completed Lucky7 project, which is located in the c:\vb05sbs\chap02\lucky7 folder on your hard disk (the default location for the Chapter 2 practice files). If you need a refresher course on opening projects, see the detailed instructions in Chapter 1. If you are asked if you want to save changes to the MyLucky7 project, be sure to click Save.
This book is a step-by-step tutorial, so you will benefit most from building the projects on your own and experimenting with them. But after you have completed the projects, it is often a good idea to compare what you have with the practice file “solution” that I provide, especially if you run into trouble. To make this easy, I will give you the name of the solution files on disk before you run the completed program in most of the step-by-step exercises.
After you have compared the MyLucky7 project to the Lucky7 solution files on disk, re-open MyLucky7, and prepare to compile it as an executable file. If you didn't create MyLucky7, use my solution file to complete the exercise.