Positioning Forms on the Windows Desktop


Positioning Forms on the Windows Desktop

You've learned how to add forms to your Visual Basic project and how to open and close forms by using program code. But which tool or setting determines the placement of forms on the Windows desktop when your program runs? As you might have noticed, the placement of forms on the screen at run time is different from the placement of forms within the Visual Studio development environment at design time. In this section, you'll learn how to position your forms just where you want them at run time so that users see just what you want them to see.

In Visual Basic 6, a graphical tool called the Form Layout window controls the placement of forms at run time. You drag a tiny form icon within the Form Layout window to where you want the final form to appear at run time, and Visual Basic records the screen coordinates you specify. In Visual Basic 2005, there's no Form Layout window, but you can still position your forms precisely on the Windows desktop.

The tool you use isn't a graphical layout window but a property named DesktopBounds that is maintained for each form in your project. DesktopBounds can be read or set only at run 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 appears 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 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 locate your forms on the Windows desktop at run time.

Use the StartPosition property to position the form

  1. Click the Close Project command on the File menu, and then create a new application project named My Desktop Bounds.

  2. If the project's form isn't visible, display it now.

  3. Click the form to display its properties in the Properties window.

  4. Set the StartPosition property to CenterScreen.

    Changing the StartPosition property to CenterScreen directs Visual Basic to display the form in the center of the Windows desktop when you run the program.

  5. Click the Start Debugging button to run the application.

    Visual Basic loads the form and displays it in the middle of the screen, as shown here:

    graphic

  6. Click the Close button on the form to stop the program.

    The IDE returns.

  7. Set the StartPosition property to Manual.

    The Manual property setting directs Visual Basic to position the form based on the values in the Location property.

  8. Set the Location property to 100, 50.

    The Location property specifies the position, in pixels, of the upper-left corner of the form.

  9. Click the Start Debugging button to run the application.

    Visual Basic loads the form and then displays it on the Windows desktop 100 pixels from the left and 50 pixels from the top, as shown here:

    graphic

  10. Click the Close button on the form to close the program.

You've experimented with a few basic StartPosition settings for positioning a form at run time. Now you'll use the DesktopBounds property to size and position a second form window while the program is running. You'll also learn how to create a new form at run time without using the Add Windows Form command on the Project menu.

Set the DesktopBounds property

  1. Use the Button control to add a button object to the form, and then change the Text property of the button object to “Create Form”.

  2. Double-click the Create Form button to display the Button1_Click event procedure in the Code Editor.

  3. Type the following program code:

    'Create a second form named form2 Dim form2 As New Form 'Define the Text property and border style of the form form2.Text = "My New Form" form2.FormBorderStyle = FormBorderStyle.FixedDialog 'Specify that the position of the form will be set manually form2.StartPosition = FormStartPosition.Manual 'Declare a Rectangle structure to hold the form dimensions 'Upper left corner of form (200, 100) 'Width and height of form (300, 250) Dim Form2Rect As New Rectangle(200, 100, 300, 250) 'Set the bounds of the form using the Rectangle object form2.DesktopBounds = Form2Rect 'Display the form as a modal dialog box form2.ShowDialog()

    When the user clicks the Create Form button, this event procedure creates a new form with the title “My New Form” and a fixed border style. To use program code to create a new form, you use the Dim statement and specify a variable name for the form and the Form class, which is automatically included in projects as part of the System .Windows.Forms namespace. You can then set properties such as Text, FormBorderStyle, StartPosition, and DesktopBounds. The StartPosition property is set to FormStartPosition.Manual to indicate that the position will be set manually. The DesktopBounds property sizes and positions the form and requires an argument of type Rectangle. The Rectangle type is a structure that defines a rectangular region and is automatically included in Visual Basic projects. Using the Dim statement, the Form2Rect variable is declared of type Rectangle and initialized with the form position and size values. At the bottom of the event procedure, the new form is opened as a dialog box using the ShowDialog method.

    Although I usually recommend placing your Dim statements together at the top of the form, here I have placed one a little lower in the code to make it easier to understand the context and use of the variable.

    TIP
    The complete Desktop Bounds program is located in the c:\vb05sbs\chap14\desktop bounds folder.

  4. Click the Start Debugging button to run the program.

    Visual Basic displays the first form on the desktop.

  5. Click the Create Form button.

    Visual Basic displays the My New Form dialog box with the size and position you specified in the program code, as shown here:

    graphic

    Notice that you can't resize the second form, because the FormBorderStyle was set to FixedDialog.

  6. Close the second form, and then close the first form.

    Your program stops running, and the IDE returns.

  7. Click the Save All button, and specify the c:\vb05sbs\chap14 folder as the location.

Minimizing, Maximizing, and Restoring Windows

In addition to establishing the size and location of a Visual Basic form, you can minimize a form to the Windows taskbar, maximize a form so that it takes up the entire screen, or restore a form to its normal shape. These settings can be changed at design time or at run time based on current program conditions.

To allow a form to be both minimized and maximized, you must first verify that the form's minimize and maximize boxes are available. Using the Properties window or program code, you specify the following settings:

MaximizeBox = True MinimizeBox = True

Then, in program code or by using the Properties window, you set the WindowState property for the form to Minimized, Maximized, or Normal. (In code, you need to add the FormWindowState constant, as shown below.) For example, the following program statement minimizes a form to the Windows taskbar:

WindowState = FormWindowState.Minimized

If you want to control the maximum or minimum size of a form, set the MaximumSize or MinimumSize properties at design time by using the Properties window. To set the MaximumSize or MinimumSize in code, you'll need to use a Size structure (which is similar to the Rectangle structure used in the previous exercise), as shown here:

Dim FormSize As New Size(400, 300) MaximumSize = FormSize



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net