Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
Chapter 14
Managing Windows Forms and Controls at Run Time
After completing this chapter, you will be able to:
Add new forms to a program and switch between multiple forms.
Change the position of a form on the Microsoft Windows desktop.
Add controls to a form at run time.
Change the alignment of objects within a form at run time.
Use the Properties dialog box to specify the startup form.
In Part II, you learned many of the core development skills necessary for writing Microsoft Visual Basic applications. You learned how to use variables, operators, and decision structures; how to manage code flow with loops, timers, procedures, and structured error handlers; how to debug your programs; and how to organize information with arrays, collections, text files, and string processing techniques.
Each exercise you have worked with so far concentrated on one or more of these core skills in a simple, stand-alone program. Real-world programs are rarely so simple. They usually require you to combine the techniques in various ways and with various enhancements. Your programs will quite often require multiple forms, used as dialog boxes, input and output forms, reports, and so on. Because Visual Basic treats each form as a separate object, you can think of them as simple building blocks that you can combine to create powerful programs.
In Part III, you'll focus again on the user interface, and you'll learn how to add multiform projects, animation effects, visual inheritance, and printing support to your Visual Basic applications.
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.
Upgrade Notes: Migrating Visual Basic 6 Code to Visual Basic 2005
If you're experienced with Microsoft Visual Basic 6, you'll notice some new features in Microsoft Visual Basic 2005, including the following:
The My.Forms object now offers an easy way to manipulate the forms in a project at run time. (In Microsoft Visual Basic .NET 2003, you cannot set the properties of a second form without declaring an instance variable of the form.)
In Visual Basic 6, you can set a form's run-time position on the Windows desktop by using the graphical Form Layout window. There's no Form Layout window in Microsoft Visual Studio 2005, but you can use a new form property named DesktopBounds to set the size and location of a form at run time.
In Visual Basic 6, you can add new controls to a form at run time by using program code. Visual Basic 2005 has a similar capability, but with some syntax changes for adding controls at run time, which will be discussed in this chapter.
With the Anchor and Dock properties introduced in Microsoft Visual Basic .NET 2002, you can create forms in which the objects are sized as the form is resized. The Anchor property for objects on a form specifies which sides should remain at a constant from the edges of the form. The Dock property forces an object to remain attached to one edge of the form.