Closing and opening a form on one monitor on two monitors

I looked around and found these two articles:

The problem is that this article is in the ball park of my problem, but I canโ€™t use them as I see it (unless I make my decision really difficult). The article talks about restoring the form when they completely closed the application, this is not quite what I'm trying to accomplish.

What I am doing is closing and opening the same form inside the same running application. When this happens, I want it to have the exact exact location, state and size, where and when I close. This is straightforward because I can save the location, state, and size from the form object, recycle it, and apply the old values โ€‹โ€‹to my new form. This works, but if I have a maximum window on monitor 2, and the close / open function starts, it opens a form maximized on monitor 1.

Is there any easy way to save it on monitor 2 in the above case, or do I need to dive into complex libraries?

+4
source share
4 answers

Following the Hans Passant instructions in the comment in the original post, and setting the values โ€‹โ€‹correctly resolved the problem. Now I do this in my Forms OnLoad event:

if(UseGivenpositioningValues) { Location = OverrideLocation; if (OverrideWindowState == FormWindowState.Normal) Size = OverrideSize; WindowState = OverrideWindowState; UseGivenpositioningValues = false; } 

Indicate the location first. This is not an ideal solution, as Justin pointed out in his answer, since the user can change his setting, and then the form may appear on the screen if the user changes the setting. However, in my particular case this is not a problem.

0
source

If I were you, I would consider your problem as a simple extension of these related issues, the only change is that your application does not close - just a window (so you do not need to save this information to disk, just save it in memory) .

The reason is that users can (and ultimately one of them is likely to) change the display configuration (number of displays, display position, etc.) while your application is running (for example, a laptop user disconnecting an external screen), and therefore, if you do not take this into account, you will find yourself in the position of your windows outside the screen, where the user will not be able to access.

+1
source

Try it...

(Form 2 is the form you want to place.) Modify if necessary.

 using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form2 : Form { static System.Drawing.Point _location = new Point(); static System.Drawing.Size _size; static FormWindowState _state; public Form2() { InitializeComponent(); this.Load += new EventHandler( Form2_Load ); this.FormClosing += new FormClosingEventHandler( Form2_FormClosing ); } void Form2_Load( object sender, EventArgs e ) { // Restore the Form position. // // Handle possibility that our previous screen location is no longer valid for // the current display environment (ie, multiple->single display system). // Point location = _location; if ( location == new Point( 0, 0 ) || !IsScreenLocationValid( location ) ) { if ( null != this.Parent ) this.StartPosition = FormStartPosition.CenterParent; else this.StartPosition = FormStartPosition.CenterScreen; } else { this.Location = location; // Ensure that the Form size is not smaller than its minimum allowed. // Size size = _size; size.Width = System.Math.Max( size.Width, this.MinimumSize.Width ); size.Height = System.Math.Max( size.Height, this.MinimumSize.Height ); this.Size = size; } // Only restore the Form window state if it is not minimized. // (If we restore it as minimized, the user won't see it). // if ( _state == FormWindowState.Normal || _state == FormWindowState.Maximized ) { this.WindowState = _state; } } /// <summary> /// Determines if the given screen location is valid for the current display system. /// </summary> /// <param name="location">A Point object describing the location</param> /// <returns>True if the location is valid; otherwise, false</returns> static bool IsScreenLocationValid( Point location ) { Rectangle screenBounds = System.Windows.Forms.Screen.GetBounds( location ); return screenBounds.Contains( location ); } void Form2_FormClosing( object sender, FormClosingEventArgs e ) { _state = this.WindowState; if ( _state == FormWindowState.Normal ) { _location = this.Location; _size = this.Size; } else { _location = this.RestoreBounds.Location; _size = this.RestoreBounds.Size; } } } } 
0
source

I created an extension method that will work if you close the application or only the current window. I call RestoreLastLocation in the form_load event and SaveLastLocation in the form_closing event. This is old code, so I apologize if its a little rude.

  public static void SaveLastLocation(this Form form, string UniqueName) { FormWindowState CurState = form.WindowState; if (CurState == FormWindowState.Minimized) CurState = FormWindowState.Normal; form.WindowState = FormWindowState.Normal; if (Properties.Settings.Default.WindowSettings == null) Properties.Settings.Default.WindowSettings = new System.Collections.Specialized.StringCollection(); if(Properties.Settings.Default.WindowSettings.Count > 0) foreach (string S in Properties.Settings.Default.WindowSettings) if (S.Split('|').First().ToLower() == UniqueName.ToLower()) { Properties.Settings.Default.WindowSettings.Remove(S); break; } Properties.Settings.Default.WindowSettings.Add(string.Format("{0}|{1}|{2}|{3}|{4}|{5}", UniqueName, form.Top.ToString(), form.Left.ToString(), form.Height.ToString(), form.Width.ToString(), form.WindowState.ToString())); Properties.Settings.Default.Save(); } public static void RestoreLastLocation(this Form form, string UniqueName) { if (Properties.Settings.Default.WindowSettings != null && Properties.Settings.Default.WindowSettings.Count > 0) foreach (string S in Properties.Settings.Default.WindowSettings) { string[] Parts = S.Split('|'); if (Parts[0].ToLower() == UniqueName.ToLower()) { form.Top = int.Parse(Parts[1]); form.Left = int.Parse(Parts[2]); form.Height = int.Parse(Parts[3]); form.Width = int.Parse(Parts[4]); form.WindowState = (FormWindowState)Enum.Parse(typeof(FormWindowState), Parts[5]); break; } } } 
0
source

All Articles