0

I want to get inside here:

Protected override void OnResize(EventArgs e)
{

}

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

spunit
  • 523
  • 2
  • 6
  • 23

2 Answers2

1

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
    }
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • I don't know what you mean, I'm not that good at programming really. But what's inside OnResize can't be placed in another function, so that's why I need to trigger OnResize when Maximize / Normal. – spunit Oct 16 '14 at 23:43
  • Why can't the code in OnResize be called instead from OnClientSizeChanged()? – Peter Duniho Oct 16 '14 at 23:55
  • How do I add OnClientSizeChanged()? I can't find it in Events. – spunit Oct 17 '14 at 00:01
  • OnClientSizeChanged() is a method on the base class. You override it by typing: `protected override void OnClientSizeChanged(EventArgs e)` – Rufus L Oct 17 '14 at 00:05
0

If you HAVE to use that override, you can just do this:

protected override void OnResize(EventArgs e)
{
    if (this.WindowState == FormWindowState.Maximized || 
        this.WindowState == FormWindowState.Normal)
    {
        // Do something here
    }

    // You should probably call the base implementation too
    base.OnResize(e);

    // Note that calling base.OnResize will trigger 
    // Form1_Resize(object sender, EventArgs e)
    // which is normally where you handle resize events
}

Alternatively, put your code in the Resize event (which is the 'normal' way to do it):

private void Form1_Resize(object sender, EventArgs e)
{
    if (this.WindowState == FormWindowState.Maximized || 
        this.WindowState == FormWindowState.Normal)
    {
        // Do something here
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43