Search code examples
c#formsresizewindowstate

Trigger OnResize when maximize / normal


I want to get inside here:

Protected override void OnResize(EventArgs e)
{

}

when I change WindowState to Maximize / Normal. How do I do this?


Solution

  • Why do you want that method specifically? The OnClientSizeChanged() method is called when the WindowState changes, and you should be able to do the same kind of work there.

    Note that if you need to actually know that the method got called because of the WindowState change as opposed to the user resizing the window, it's simple enough to add a private field that is set to the current WindowState value, and in the OnClientSizeChanged() method, compare that field with the actual WindowState value.

    For example:

    private FormWindowState _lastWindowState;
    
    protected override void OnClientSizeChanged(EventArgs e)
    {
        base.OnClientSizeChanged(e);
    
        if (WindowState != _lastWindowState)
        {
            _lastWindowState = WindowState;
    
            // Do whatever work here you would have done in OnResize
        }
    }