Click here to Skip to main content
13,588,909 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


32 bookmarked
Posted 19 Feb 2009
Licenced CPOL

Saving and Restoring Form Location in a Multi-Monitor Environment

, 19 Feb 2009
Rate this:
Please Sign up or sign in to vote.
This article describes a method to save and restore form locations that will continue to work when display settings change.


Many applications store the size and location of a form so that the next time it is opened it restores to the same size and location. This is a great feature, but can present a problem when the display environment is not always the same. For example, a notebook computer may be connected to a secondary monitor at the office, but not when on the road. A form that is set to load on the secondary monitor will be displayed off the visible screen when the notebook is on the road. The code in this article shows how to get the benefits of this feature without this problem.

A quick search of the Internet will find many ways to save the form position. None that I found seemed to take multiple monitors (or more precisely, an inconsistent monitor setup) into account. I also easily found a code example that allowed me to specify which monitor to start a form on. The problem with this code is that it assumes that the coordinate system is based on position 0,0 being the top left corner of the specified monitor, not necessarily the top left corner of the primary monitor, while the form coordinates are given based on the primary monitor. So, if I positioned the form in the middle of my left (secondary) monitor and exited, I would store an X position of -600 (600 pixels to the left of the primary monitor), but when loading the form and specifying the startup monitor, I need to tell it to move 424 pixels to the right from its origin on the secondary monitor. All very confusing.

The key was to determine the bounds of the monitor used. This uses the same coordinates referenced in the form's location, so a 1024×768 monitor to the left of the primary monitor (also 1024×768) would have its upper left corner be -1024,0. We can subtract the bounds coordinates from the saved coordinates to find the coordinates relative to the top left corner of the monitor used. I also check to verify that the position stored is within the bounds of the selected monitor, and if not, display the form in the upper left corner of the selected monitor. This way, the form is never displayed in a position where it can not be seen.

Another problem with the code I found for specifying which screen to start on was that it did not verify the existence of that screen before trying to change to it. So, if a monitor was disconnected, you would end up with an error. Storing the selected monitor also wound up being a bit more of a challenge than expected. The property Screen.DisplayName pads the end of the string with a number of hidden characters. Trimming off the extra characters solved my problems with this section, but it was annoying to track down.

Using the code

The code is in two segments. The first is placed in the Form_Load procedure, and the second in the Form_FormClosing procedure, as marked. The example shown stores the form position in an INI file. An XML file or the Registry could be used as easily. The ReadINI and WriteINI procedures referenced in the code are not shown here. There are many methods available for performing these functions.

The following code goes in the Form_Load procedure:

'Multi Monitor Aware - Restore Postition to last Used
Dim posScreen As Integer
Dim posX As Integer
Dim posY As Integer
Dim screen As Screen
' Read the previous screen used from the INI file
posScreen = Val(ReadIni(File, "Main", "PosScreen"))
screen = screen.AllScreens(0)
' Verify the Specified screen exists, if so then use it
If screen.AllScreens.Length > posScreen + 1 Then
  screen = screen.AllScreens(0)
  screen = screen.AllScreens(posScreen)
End If
' Read the Position from the INI File
posX = Val(ReadIni(File, "Main", "PosX"))
posY = Val(ReadIni(File, "Main", "PosY"))
Dim pt As New Point(posX, posY)
If screen.Bounds().Contains(pt) Then
  pt.X = posX - screen.Bounds().X
  pt.Y = posY - screen.Bounds().Y
  pt.X = 0
  pt.Y = 0
End If
Me.StartPosition = FormStartPosition.Manual
Me.Location = screen.Bounds.Location + pt

The following code goes in the Form_FormClosing procedure:

' Save Screen Position
For i As Integer = 0 To
screen.AllScreens().Length - 1
  If stringtoasc(screen.AllScreens(i).DeviceName.ToString) _
  = stringtoasc(screen.FromControl(Me).DeviceName.ToString) Then
    WriteIni(File, "Main", "PosScreen", i)
  End If
WriteIni(File, "Main", "PosX", Me.Location.X)
WriteIni(File, "Main", "PosY", Me.Location.Y)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Questionsimplify/eliminate stringtoasc Pin
TomWpoint12-Oct-15 4:29
memberTomWpoint12-Oct-15 4:29 
BugBroken if Window is Maximized & missing code Pin
chrisf865723-Mar-15 22:37
memberchrisf865723-Mar-15 22:37 
GeneralMy vote of 4 Pin
Member 69720399-Nov-12 9:41
memberMember 69720399-Nov-12 9:41 
GeneralGreat topic Pin
scottb224-Feb-09 5:03
memberscottb224-Feb-09 5:03 
GeneralI have an alternative to this solution, please read further for more information Pin
PHenry20-Feb-09 17:16
memberPHenry20-Feb-09 17:16 
GeneralRe: I have an alternative to this solution, please read further for more information Pin
NealSchafer13-Mar-09 16:11
memberNealSchafer13-Mar-09 16:11 
GeneralRe: I have an alternative to this solution, please read further for more information Pin
Pete BSC9-Jun-09 5:31
memberPete BSC9-Jun-09 5:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180615.1 | Last Updated 19 Feb 2009
Article Copyright 2009 by NealSchafer
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid