Dela via


How to: Set the Screen Location of Windows Forms

You can specify where a form is to be displayed on the computer screen by entering values in the Location property. This specifies the position, in pixels, of the top-left corner of the form. Also, you need to set the StartPositionproperty to indicate the boundaries of the display area.

Note

Keep in mind that screen size and resolution often vary depending on the user's system. Additionally, systems that have multiple monitors attached may have trouble recognizing the boundaries of the display area. These two situations will often cause a form's location to change unpredictably, despite the Location property setting. For this reason, the default setting for the StartPosition property for a Windows Application is WindowsDefaultLocation, which tells the operating system to compute the best location for the form at startup, based on the current hardware. Another alternative is to set the StartPosition property to Center and then change the location of the form in code. See "To position forms programmatically" below for more information.

To position forms using the Properties window

  1. In the Properties window, choose the form from the drop-down. Set the form's StartPosition property to Manual.

  2. Type the values for the Location property, separated by a comma, to position the form, where the first number (X) is the distance from the left border of the display area and second number (Y) is the distance from the upper border of the display area.

    Note

    Expand the Location property to enter the X and Y property values individually.

To position forms programmatically

  • Define the location of a form at run time by setting the Location property of the form to a Point, as shown in the following example:

    Form1.Location = New Point(100, 100)
    
    Form1.Location = new Point(100, 100);
    
    Form1.set_Location(new Point(100, 100));
    
    Form1->Location = Point(100, 100);
    

    -or-

    Change the X coordinate or Y coordinate of the form's location using the Left property (for the X coordinate) and the Top property (for the Y coordinate). The following example adjusts the form's X coordinate to the 300 pixel point:

    Form1.Left = 300
    
    Form1.Left = 300;
    
    Form1.set_Left(300);
    
    Form1->Left = 300;
    

To change form position programmatically by increments

  • Increment the X coordinate of the form using the Left property. The following example adjusts the form's X coordinate by 200 pixels:

    Form1.Left += 200
    
    Form1.Left += 200;
    
    Form1.set_Left(Form1.get_Left() + 200);
    
    Form1->Left += 200;
    

    Note

    Use the Location property to set a Windows Form's X and Y positions simultaneously. To set them individually, use the form's Left (X) or Top (Y) property. Do not try to implicitly set the X and Y coordinates of the Point structure that represents the form's location, because this contains a copy of the form's coordinates.

    In lieu of using the Location property, the DesktopLocation property can be used to set the location of your form. This property sets the location of your form relative to the taskbar and is useful if the taskbar has been docked to the top or left of the user's monitor. Docking the taskbar in this fashion obscures the desktop coordinates (0,0). A form with the DesktopLocationproperty set to (0, 0) always appears in the upper left corner of the primary monitor, but not behind the taskbar.

To set the Desktop Location property programmatically

  • Set the DesktopLocation property as you would any other property. The following example establishes a new location for an Accounts form.

    Dim frmAccounts as new Form()
    Set FrmAccounts.DesktopLocation = new Point(100,100)
    
    Form frmAccounts= new Form();
    frmAccounts.DesktopLocation = new Point(100,100);
    
    Form frmAccounts =  new Form();
    frmAccounts.set_DesktopLocation(new Point(100, 100));
    
    Form^ frmAccounts= gcnew Form();
    frmAccounts->DesktopLocation = Point(100,100);
    

    Note

    The DesktopLocation property does not appear in the Properties window and can only be set in code.

See Also

Reference

Windows Forms Overview

Other Resources

Creating a New Windows Form

Arranging Controls on Windows Forms