Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 246137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:08:50+00:00 2026-05-11T21:08:50+00:00

Many posts around about restoring a WinForm position and size. Examples: www.stackoverflow.com/questions/92540/save-and-restore-form-position-and-size www.codeproject.com/KB/dialog/restoreposition.aspx?fid=1249382&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2595746 But

  • 0

Many posts around about restoring a WinForm position and size.

Examples:

  • http://www.stackoverflow.com/questions/92540/save-and-restore-form-position-and-size
  • http://www.codeproject.com/KB/dialog/restoreposition.aspx?fid=1249382&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2595746

But I have yet to find code to do this with multiple monitors.

That is, if I close my .NET Winform app with the window on monitor 2, I want it to save the windows size, location, and state to the application settings, so it could later restore to monitor 2 when I restart the app. It would be nice if, like in the codeproject example above, it includes some sanity checks, as in if the saved location is mostly off-screen it “fixes” it. Or if the saved location is on a monitor that is no longer there (e.g. my laptop is now by itself without my second monitor) then it correctly moves it to monitor 1.

Any thoughts?

My environment: C#, .NET 3.5 or below, VS2008

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:08:51+00:00Added an answer on May 11, 2026 at 9:08 pm

    The answer provided by VVS was a great help! I found two minor issues with it though, so I am reposting the bulk of his code with these revisions:

    (1) The very first time the application runs, the form is opened in a Normal state but is sized such that it appears as just a title bar. I added a conditional in the constructor to fix this.

    (2) If the application is closed while minimized or maximized the code in OnClosing fails to remember the dimensions of the window in its Normal state. (The 3 lines of code–which I have now commented out–seems reasonable but for some reason just does not work.) Fortunately I had previously solved this problem and have included that code in a new region at the end of the code to track window state as it happens rather than wait for closing.


    With these two fixes in place, I have tested:

    A. closing in normal state–restores to same size/position and state

    B. closing in minimized state–restores to normal state with last normal size/position

    C. closing in maximized state–restores to maximized state and remembers its last size/position when one later adjusts to normal state.

    D. closing on monitor 2–restores to monitor 2.

    E. closing on monitor 2 then disconnecting monitor 2–restores to same position on monitor 1

    David: your code allowed me to achieve points D and E almost effortlessly–not only did you provide a solution for my question, you provided it in a complete program so I had it up and running almost within seconds of pasting it into Visual Studio. So a big thank you for that!

    public partial class MainForm : Form
    {
        bool windowInitialized;
    
        public MainForm()
        {
            InitializeComponent();
    
            // this is the default
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.WindowsDefaultBounds;
    
            // check if the saved bounds are nonzero and visible on any screen
            if (Settings.Default.WindowPosition != Rectangle.Empty &&
                IsVisibleOnAnyScreen(Settings.Default.WindowPosition))
            {
                // first set the bounds
                this.StartPosition = FormStartPosition.Manual;
                this.DesktopBounds = Settings.Default.WindowPosition;
    
                // afterwards set the window state to the saved value (which could be Maximized)
                this.WindowState = Settings.Default.WindowState;
            }
            else
            {
                // this resets the upper left corner of the window to windows standards
                this.StartPosition = FormStartPosition.WindowsDefaultLocation;
    
                // we can still apply the saved size
                // msorens: added gatekeeper, otherwise first time appears as just a title bar!
                if (Settings.Default.WindowPosition != Rectangle.Empty)
                {
                    this.Size = Settings.Default.WindowPosition.Size;
                }
            }
            windowInitialized = true;
        }
    
        private bool IsVisibleOnAnyScreen(Rectangle rect)
        {
            foreach (Screen screen in Screen.AllScreens)
            {
                if (screen.WorkingArea.IntersectsWith(rect))
                {
                    return true;
                }
            }
    
            return false;
        }
    
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
    
            // only save the WindowState if Normal or Maximized
            switch (this.WindowState)
            {
                case FormWindowState.Normal:
                case FormWindowState.Maximized:
                    Settings.Default.WindowState = this.WindowState;
                    break;
    
                default:
                    Settings.Default.WindowState = FormWindowState.Normal;
                    break;
            }
    
            # region msorens: this code does *not* handle minimized/maximized window.
    
            // reset window state to normal to get the correct bounds
            // also make the form invisible to prevent distracting the user
            //this.Visible = false;
            //this.WindowState = FormWindowState.Normal;
            //Settings.Default.WindowPosition = this.DesktopBounds;
    
            # endregion
    
            Settings.Default.Save();
        }
    
        # region window size/position
        // msorens: Added region to handle closing when window is minimized or maximized.
    
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            TrackWindowState();
        }
    
        protected override void OnMove(EventArgs e)
        {
            base.OnMove(e);
            TrackWindowState();
        }
    
        // On a move or resize in Normal state, record the new values as they occur.
        // This solves the problem of closing the app when minimized or maximized.
        private void TrackWindowState()
        {
            // Don't record the window setup, otherwise we lose the persistent values!
            if (!windowInitialized) { return; }
    
            if (WindowState == FormWindowState.Normal)
            {
                Settings.Default.WindowPosition = this.DesktopBounds;
            }
        }
    
        # endregion window size/position
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are many many posts and an infinite number of questions on how to
I read many posts about this problem like [this link][1] and one solution is
There are already quite some posts about the Singleton-Pattern around, but I would like
There are many posts about Devise and the lack of availability of current_user for
Site I'm talking about is here: https://www.facebook.com/kleenexau/app_295074103899059 If you run it in popular browsers
I have gone through many posts on SO regarding this issue: Tried everything in
I have seen many posts on this subject, but none have been answered, and
I've looked at so many posts on what inflation is for android and I
I have a User model that has many posts. Im trying to render the
I have a team site blog with many posts that I wish to export.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.