Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# window height only gets bigger, not smaller

I've got a form with a collapsible group box (got it from http://www.codeproject.com/Articles/12835/XP-Style-Collapsible-GroupBox). I'm using it basically to show/hide advanced options on the form. The group-box itself is not at issue, however, as I did the same thing with just a regular Panel that I changed the Visible property on and the issue is the same:

When I hide this box, I want the window to shrink accordingly. So I've got code when you click on the box header that basically goes like:

if (OptionsPanel.IsCollapsed)
    this.Height -= (OptionsPanel.Height - OptionsPanel.CollapsedHeight);
else
    this.Height += (OptionsPanel.Height - OptionsPanel.CollapsedHeight);

The problem is- when you expand the box, the window does indeed grow accordingly, but when you collapse the box, it doesn't shrink. In fact, nothing I do seems able to make the window smaller - I've even tried hard-coding the window sizes, and it never gets smaller, only bigger.

Is there something I'm missing to make this work? Or is there a better component out there that will do this right? Honestly I'm shocked that there isn't a default control in the Toolbox for doing this sort of thing, as it seems like a fairly common thing to do, and I've seen things like this on a lot of forms. Doesn't seem like the kind of thing you should have to go to 3rd party plugins or roll your own code for.

like image 258
Darrel Hoffman Avatar asked Nov 29 '25 16:11

Darrel Hoffman


2 Answers

Did you try adding AutoSizeMode=GrowAndShrink property?

Preferably, both AutoSize=true, AutoSizeMode=GrowAndShrink

In C#

OptionsPanel.AutoSize = true;
OptionsPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink;

Reference: http://msdn.microsoft.com/en-us/library/system.windows.forms.autosizemode.aspx

like image 111
stan Avatar answered Dec 02 '25 06:12

stan


Make sure your Form's AutoSize is set to True and AutoSizeMode set to GrowAndShrink the default is GrowOnly

enter image description here

like image 29
Mark Hall Avatar answered Dec 02 '25 06:12

Mark Hall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!