How to detect when a WinForms Form is Maximized

The Form.FormWindowState enumeration (will either be Maximized, Minimized or Normal).

A more subtle question is when to detect that the form has been maximized. In my case I needed a way to detect when a form was maximized and/or minimized exactly once because the operation to be performed was somewhat expensive.

Control.Layout event to the rescue! It's raised whenever a control needs to adjust its layout (e.g., resized, child control added or removed, etc...). If one of your controls doesn't know how to lay itself out (because, for example, you're handling its positioning) then the trick is to handle its parent container's Layout event.

By using the LayoutEventArgs.AffectedControl you can make sure that the expensive operation is only performed when its container control is affected.

No comments:

Post a Comment