LocationChanged to set windowstate=Maximized
-
How do I implement a Window app to maximize when user moves the window from one display monitor to another. When the move occurs LocationChanged event is triggered and the window state becomes Normal. I tried this.WindowState=FormWindowState.Maximized in the LocationChanged handler. However, the window does not maximized. Likely because it triggered circular reference. I had also implemented a module Boolean variable to avoid this. However, the window never maximized. What is the proper way to implement this? Thank you in advance for your response.
-
How do I implement a Window app to maximize when user moves the window from one display monitor to another. When the move occurs LocationChanged event is triggered and the window state becomes Normal. I tried this.WindowState=FormWindowState.Maximized in the LocationChanged handler. However, the window does not maximized. Likely because it triggered circular reference. I had also implemented a module Boolean variable to avoid this. However, the window never maximized. What is the proper way to implement this? Thank you in advance for your response.
While the mouse button is down, as far as the system is concerned, the window is still moving. I would look at setting a flag in the LocationChanged event to let you know that the window is moving, and then set the FormWindowState in the MouseUp event (and then resetting that flag too, of course).
Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
-
How do I implement a Window app to maximize when user moves the window from one display monitor to another. When the move occurs LocationChanged event is triggered and the window state becomes Normal. I tried this.WindowState=FormWindowState.Maximized in the LocationChanged handler. However, the window does not maximized. Likely because it triggered circular reference. I had also implemented a module Boolean variable to avoid this. However, the window never maximized. What is the proper way to implement this? Thank you in advance for your response.
I think the ResizeBegin and ResizeEnd events will help you. The naming is misleading as the purpose of these events is to indicate when the form is showing the resizing border. Click and hold on the form border or caption bar and the ResizeBegin event is raised. ResizeEnd is seen on release whether or not any resizing has occurred. Here is a trivial (and annoying) modification to a form to make it maximise when it is moved without having changed it's size.
private Rectangle? lastBounds; protected override void OnResizeBegin(EventArgs e) { base.OnResizeBegin(e); System.Diagnostics.Debug.Print("EVENT ResizeBegin"); lastBounds = Bounds; } protected override void OnResizeEnd(EventArgs e) { base.OnResizeBegin(e); System.Diagnostics.Debug.Print("EVENT ResizeEnd"); if (lastBounds.HasValue) { Rectangle previous = lastBounds.Value; // Maximise when moved without resizing if (previous != Bounds && previous.Size == Bounds.Size) { WindowState = FormWindowState.Maximized; } lastBounds = null; } }
In your case it would be necessary to detect the System.Windows.Forms.Screen containing the form in ResizeBegin and test for the desired destination Screen in ResizeEnd. Alan.
-
I think the ResizeBegin and ResizeEnd events will help you. The naming is misleading as the purpose of these events is to indicate when the form is showing the resizing border. Click and hold on the form border or caption bar and the ResizeBegin event is raised. ResizeEnd is seen on release whether or not any resizing has occurred. Here is a trivial (and annoying) modification to a form to make it maximise when it is moved without having changed it's size.
private Rectangle? lastBounds; protected override void OnResizeBegin(EventArgs e) { base.OnResizeBegin(e); System.Diagnostics.Debug.Print("EVENT ResizeBegin"); lastBounds = Bounds; } protected override void OnResizeEnd(EventArgs e) { base.OnResizeBegin(e); System.Diagnostics.Debug.Print("EVENT ResizeEnd"); if (lastBounds.HasValue) { Rectangle previous = lastBounds.Value; // Maximise when moved without resizing if (previous != Bounds && previous.Size == Bounds.Size) { WindowState = FormWindowState.Maximized; } lastBounds = null; } }
In your case it would be necessary to detect the System.Windows.Forms.Screen containing the form in ResizeBegin and test for the desired destination Screen in ResizeEnd. Alan.
Hi Mick, Alan MouseUp and ResizeBegin/End works wonderfully. Thank you and Happy New Year.