Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

352 Part III Designing the User Interface


How Forms Are Used


Microsoft Visual Basic gives you significant flexibility when using forms. You can make all
the forms in a program visible at the same time, or you can load and unload forms as the
program needs them. If you display more than one form at once, you can allow the user to
switch between the forms, or you can control the order in which the forms are used. A form
that must be addressed when it’s displayed on the screen is called a dialog box. Dialog boxes
(sometimes called modal forms) retain the focus until the user clicks OK, clicks Cancel, or
otherwise dispatches them. To display an existing form as a dialog box in Visual Basic, you
open it by using the ShowDialog method.

If you want to display a form that the user can switch away from, you use the Show method
instead of the ShowDialog method. (Forms that can lose the application focus are sometimes
also called non-modal forms or modeless forms .) Most Windows applications use regular,
non-modal forms when displaying information because they give the user more flexibility, so
this style is the default when you create a new form in Microsoft Visual Studio. Because forms
are simply members of the System.Windows.Forms.Form class, you can also create and display
forms by using program code.

Working with Multiple Forms


The following exercises demonstrate how you can use a second form to display Help
information for the Lucky Seven program that you worked with in Chapter 2, “Writing Your
First Program,” and Chapter 10, “Creating Modules and Procedures .” You’ll add a second
form by using the Add Windows Form command on the Project menu, and you’ll display the
form in your program code by using the My namespace and the ShowDialog method. The
second form will display a short Readme .txt file that I created to display help and copyright
information for the program (the type of information you typically see in an About or a Help
dialog box).

Add a second form


  1. Start Visual Studio, and then open the Lucky Seven Help project in the C:\Vb10sbs\
    Chap14\Lucky Seven Help folder.
    The Lucky Seven Help project is the same slot machine game that you built in
    Chapter 10. The program uses a module and a function to calculate the win rate as
    you try to spin one or more 7s.

  2. Display the primary form (LuckySeven .vb) in the Designer, if it isn’t already visible.

  3. Click the Add Windows Form command on the Project menu to add a second form to
    the project.

Free download pdf