351
Chapter 14
Managing Windows Forms
and Controls at Run Time
After completing this chapter, you will be able to:
n Add new forms to a program and switch between multiple forms.
n Change the position of a form on the Windows desktop.
n Add controls to a form at run time.
n Change the alignment of objects within a form at run time.
n Use the Project Designer to specify the startup form.
In this chapter, you’ll learn how to add additional forms to an application to handle input,
output, and special messages. You’ll also learn how to use the Me and My.Forms objects
to switch between forms, how to use the DesktopBounds property to resize a form, how
to add Toolbox controls to a form at run time, how to change the alignment of objects within
a form, and how to specify which form runs when a program is started.
Adding New Forms to a Program
Each program you’ve written so far has used one form and a series of general-purpose
dialog boxes for input and output. In many cases, dialog boxes and a form are sufficient for
communicating with the user. But if you need to exchange more information with the user in
a more customized manner, you can add additional forms to your program. Each new form is
considered an object that inherits its capabilities from the System.Windows.Forms.Form class.
By default, the first form in a program is named Form1 .vb. Subsequent forms are named
Form2 .vb, Form3 .vb, and so on. (You can change the specific name for a form by using the
Add New Item dialog box or by using Solution Explorer .) Each new form has a unique name
and its own set of objects, properties, methods, and event procedures.
Table 14-1 lists several practical uses for additional forms in your programs.
TABLE 14-1 Practical Uses for Extra Forms
Form or Forms Description
Introductory form A form that displays a welcome message, artwork, or copyright information
when the program starts
Program instructions A form that displays information and tips about how the program works
Dialog boxes Custom dialog boxes that accept input and display output in the program
Document contents A form that displays the contents of one or more files and artwork used in
the program