How do I trigger something whenever the WindowState changes in C#?

Jon picture Jon · Sep 4, 2012 · Viewed 20.6k times · Source

So I want to instantly, as this portion of the program relies on speed, trigger a function when the windowstate is changed in my main form. I need it to be something like this:

private void goButton_Click(object sender, EventArgs e)
{
   //Code
}

I checked through the events tab of the form, I have no WindowStateChanged, etc. How do I do this?

The form will be resized a lot, so checking when the size changes won't work.

Answer

Reed Copsey picture Reed Copsey · Sep 4, 2012

The Resize event (or SizeChanged) will fire when the WindowState changes.


On a side note, WPF does include a StateChanged event for this directly.