0
votes

I want to change the size of my form when deactivated (click out of it) and reset it when activated (click back in). To reset it, I have global variables called preferredHeight and preferredWidth that I am setting in MainForm_SizeChanged (which is for if the user manually resizes the form). But, when I set the new height and width, the SizeChanged method gets called! Is there any way to distinguish between whether a user triggered the SizeChanged event or whether my program did? Alternatively, is there a better way to reset the size of my form? Thanks!

    private void MainForm_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        preferredHeight = MainForm.Height;
        preferredWidth = MainForm.Width;
    }

    private void MainForm_Deactivated(object sender, EventArgs e)
    {
        MainGrid.RowDefinitions[3].Height = new GridLength(0);
        MainGrid.RowDefinitions[4].Height = new GridLength(0);
        MainForm.ResizeMode = ResizeMode.NoResize;
        MainForm.Height = 100;
        MainForm.Width = 100 * imageWidth / imageHeight;
    }

    private void MainForm_Activated(object sender, EventArgs e)
    {
        MainGrid.RowDefinitions[3].Height = new GridLength(1, GridUnitType.Star);
        MainGrid.RowDefinitions[4].Height = new GridLength(2, GridUnitType.Star);
        MainForm.ResizeMode = ResizeMode.CanResize;
        MainForm.Width = preferredWidth;
        MainForm.Height = preferredHeight;
    }
1

1 Answers

1
votes

If the user changes the size of the form then the form will still have focus.

If the user clicks out of the form then the form will have lost focus.

You can therefore check to see if the form still has focus. If it has then it's a user resize, if not then it's your "deactivating" resize that's made the call.