Export (0) Print
Expand All
16 out of 26 rated this helpful - Rate this topic

Setting the Screen Location of Windows Forms

Visual Studio .NET 2003

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 StartPosition property 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 subproperty 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:
    ' Visual Basic
    Form1.Location = New Point(100, 100)
    
    // C#
    Form1.Location = new Point(100, 100);
    
    // C++
    Form1->Location = Point(100, 100);
    

    -or-

  • Change the X coordinate or Y coordinate of the form's location using the Left subproperty (for the X coordinate) and the Top subproperty (for the Y coordinate). The following example adjusts the form's X coordinate to the 300 pixel point:
    ' Visual Basic
    Form1.Left = 300
    
    // C#
    Form1.Left = 300;
    
    // C++
    Form1->Left = 300;
    

To change form position programmatically by increments

  • Increment the X coordinate of the form using the Left subproperty. The following example adjusts the form's X coordinate by 200 pixels:
    ' Visual Basic
    Form1.Left += 200
    
    // C#
    Form1.Left += 200;
    
    // C++
    Form1->Left += 200;
    

    Tip   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) subproperty. 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 DesktopLocation property 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.
    ' Visual Basic
    Dim frmAccounts as new Form()
    Set FrmAccounts.DesktopLocation = new Point(100,100)
    
    // C#
    Form frmAccounts= new Form();
    frmAccounts.DesktopLocation = new Point(100,100);
    
    // C++
    Form * frmAccounts= new 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

Introduction to Windows Forms | Creating Windows Forms |Arranging Controls on Windows Forms

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.