Is it possible to use WinForms layout engine to set up a form in such a way that the height is adjusted completely automatically to fit the content, but the width would be adjustable by the user?
As an example, consider the following somewhat contrived form:
TLP
: TableLayoutPanel
, with 1 row and 2 columns.label1
: Label
, contained in the left column. Set Text
to something long, AutoSize
to false, Dock
to Fill.button1
: Button, contained in the right column. Set AutoSize
to true.TLP
to AutoSize
true, AutoSizeMode
to GrowAndShrink.At this point, the TLP
could reasonably be resized horizontally, but WinForms doesn't seem to allow this.
The idea is that the form containing this TLP would grow or shrink vertically based on the width, which the user can change by resizing the window as usual.
Hopefully you have got the solution by now. But in case anybody having a same query, You can set width of Maximum Size Property to the Actual Width of the Form and the Height of MaximumSize to 0.
This is entirely possible. To do this, you have to override SetBoundsCore
on your form. In this you can then control the height such that it doesn't change when a resize might want it to. Just store the height you want it to be in a variable and then have your override always set it to that. This way, when you auto-size the height, you save the new height aware before setting form Height
and SetBoundsCore
will honour that new height.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With