360 Part III Designing the User Interface
time, and it takes the dimensions of a rectangle as an argument—that is, two point pairs
that specify the coordinates of the upper-left corner of the window and the lower-right
corner of the window. The coordinate points are expressed in pixels, and the distances to the
upper-left and lower-right corners are measured from the upper-left corner of the screen.
(You’ll learn more about the Visual Basic coordinate system in the next chapter .) Because the
DesktopBounds property takes a rectangle structure as an argument, you can set both the
size and the location of the form on the Windows desktop.
In addition to the DesktopBounds property, you can use a simpler mechanism with fewer
capabilities to set the location of a form at design time. This mechanism, the StartPosition
property, positions a form on the Windows desktop by using one of the following property
settings: Manual, CenterScreen, WindowsDefaultLocation, WindowsDefaultBounds, or
CenterParent. The default setting for the StartPosition property, WindowsDefaultLocation,
lets Windows position the form on the desktop where it chooses—usually the upper-left
corner of the screen.
If you set StartPosition to Manual, you can manually set the location of the form by using
the Location property, in which the first number (x) is the distance from the left edge of the
screen and the second number (y) is the distance from the top edge of the screen. (You’ll
learn more about the Location property in the next chapter .) If you set StartPosition to
CenterScreen, the form opens in the middle of the Windows desktop. (This is my preferred
StartPosition setting .) If you set StartPosition to WindowsDefaultBounds, the form is resized to
fit the standard window size for a Windows application, and then the form is opened in the
default location for a new Windows form. If you set StartPosition to CenterParent, the form
is centered within the parent form. This final setting is especially useful in so-called multiple
document interface (MDI) applications in which parent and child windows have a special
relationship.
The following exercises demonstrate how you can set the StartPosition and DesktopBounds
properties to position a Visual Basic form. You can use either technique to position your
forms on the Windows desktop at run time.
Use the StartPosition property to position the form
- Click the Close Project command on the File menu, and then create a new Windows
Forms Application project named My Desktop Bounds. - If the project’s form isn’t visible, display it now.
- Click the form to display its properties in the Properties window.
- Set the StartPosition property to CenterScreen.