by Mike McIntyre
Setting the location of a form seems such a simple thing, but as DevCity Leader Mike McIntyre shows you, there are more combinations and points to consider than you may at first realise.
Opening and positioning forms on a user's screen is a common task for VB.NET Windows Forms programmers. This article provides VB.NET newies an introduction to this programming task. For VB.OLD programmers the article includes explanations of how positioning forms has changed from VB6 to VB.NET.
The Form properties StartLocation and Location control where the Form will be located when it is first opened. Both properties can be set at design time through the Form property grid. These properties may also be set programmatically at runtime through code added to the form's initialization block or to its Load handler.
StartLocation Property
StartLocation is a .NET enumeration that represents the five possible start positions for a Windows Form form.
- CenterParent : The form is centered within the bounds of its parent form.
- CenterScreen: The form is centered on the current display, and has the dimensions specified in the form's size.
- Manual: The location and size of the form will determine its starting position.
- WindowsDefaultBounds: The form is positioned at the Windows default location and has the bounds determined by Windows default.
- WindowsDefaultLocation: The form is positioned at the Windows default location and has the dimensions specified in the form's size.
Location Property
Location is the position of the top left corner of a Form. A form's Location property is a point consisting of x- and y-coordinates that defines a point on the computer screen.
Using StartLocation and Location
VB.NET uses a combination of StatupLocation and Location to position a form when it is first opened. Shown next are four examples that demonstrate how different combinations of StartLocation and Location affect the start position and sometimes the size of a form when it is first opened.
Example One - Intial Settings
StartLocation = Manual
Location = 0,0
Size = 200,200
Location and Size When Form Opens
Location: {X=0,Y=0}
Size: {Width=200, Height=200}
Example Two - Initial Settings
StartLocation = Center
Location = 0,0
Size = 200,200
Location and Size When Form Opens
Location: {X=412,Y=267}
Size: {Width=200, Height=200}
Example Three - Initial Settings
StartLocation = WindowsDefaultBounds
Location = 0,0
Size = 200,200
Location and Size When Form Opens
Location: {X=88,Y=116}
Size: {Width=768, Height=527}
Example Four - Initial Settings
StartLocation = WidowsDefaultLocation
Location = 0,0
Size = 200,200
Location and Size When Form Opens
Location: {X=22,Y=29}
Size: {Width=200, Height=200}
Note:
Different Combinations; Different Results
Though the Location and Size properties of the form in all examples above are set the same at design time, the actual location and size of the form when it opens varies depending on what StartLocation enumerator is used.
Because screen size and resolution can vary from system to system, WindowsDefaultLocation is used by most Windows applications. This tells the operating system to use the best location for the form at startup, based on the current hardware. If your application will run on systems with different screen sizes, resolutions, and/or multiple monitors consider using WindowsDefaultLocation as the StartPosition for your main form.
VB.NET vs VB6
In VB6, the initial screen location for a form could be specified by setting the Left and Top properties of a form. Left and Top are obsolete in VB.NET. Instead Location represents the X-Y (Left-Top) coordinate of the upper left corner of a form.
In VB6 one could set the StartUpPosition property to allow Windows to automatically set a location for the form when it first opened. The default setting was Manual, meaning that the Left and Top properties took precedence. In VB.NET the StartPosition property performs the same function. The default setting is now WindowsDefaultLocation, meaning that the Location property is ignored.
For more information about setting the screen location of Windows Forms click here.