none
Winforms WindowState changed. How to detect this? RRS feed

  • Question

  • Hi, I have a winforms MDI application where I wanted to detect when a user toggles between the MDI child(s) being maximised, and being "restored" (ie they exist as "floating MID child windows within the MDI frame)

    I am amazed there does appear to be any "OnWindowState" event. I did wire up the SizeChanged event on each child form, but of course this fired every time a child was resized. I really only wanted to know when the WindowState went back to maximised.

    Does anyone know of a more accurate way to do this than using the child MDI form's SizeChanged event?

    Thanks in advance for any adivice,
    regards Peter
    Thursday, August 14, 2008 5:29 AM

Answers

  • It's easy enough to add:

      public partial class Form1 : Form {
        private FormWindowState mLastState;
        public Form1() {
          InitializeComponent();
          mLastState = this.WindowState;
        }
        protected override void OnClientSizeChanged(EventArgs e) {
          if (this.WindowState != mLastState) {
            mLastState = this.WindowState;
            OnWindowStateChanged(e);
          }
          base.OnClientSizeChanged(e);
        }
        protected void OnWindowStateChanged(EventArgs e) {
          // Do your stuff
        }
      }


    Hans Passant.
    Thursday, August 14, 2008 10:04 AM
    Moderator

All replies

  • It's easy enough to add:

      public partial class Form1 : Form {
        private FormWindowState mLastState;
        public Form1() {
          InitializeComponent();
          mLastState = this.WindowState;
        }
        protected override void OnClientSizeChanged(EventArgs e) {
          if (this.WindowState != mLastState) {
            mLastState = this.WindowState;
            OnWindowStateChanged(e);
          }
          base.OnClientSizeChanged(e);
        }
        protected void OnWindowStateChanged(EventArgs e) {
          // Do your stuff
        }
      }


    Hans Passant.
    Thursday, August 14, 2008 10:04 AM
    Moderator
  • @NoBugz: Works perfectly!

    Thx!

    Thursday, August 26, 2010 10:30 PM
  • Great cool, thankyou very much for that!

    Peter

    Friday, August 27, 2010 1:20 AM
  • Can someone show me how to detect a WindowState change using code for VB.NET 2008 ?

    Thanks

    Thursday, April 12, 2012 1:39 AM
  • Public Partial Class Form1
    	Inherits Form
    	Private mLastState As FormWindowState
    	Public Sub New()
    		InitializeComponent()
    		mLastState = Me.WindowState
    	End Sub
    	Protected Overrides Sub OnClientSizeChanged(e As EventArgs)
    		If Me.WindowState <> mLastState Then
    			mLastState = Me.WindowState
    			OnWindowStateChanged(e)
    		End If
    		MyBase.OnClientSizeChanged(e)
    	End Sub
    	Protected Sub OnWindowStateChanged(e As EventArgs)
    		' Do your stuff
    	End Sub
    End Class

    • Proposed as answer by Cumbrowski Sunday, June 8, 2014 9:56 PM
    Sunday, June 8, 2014 9:51 PM