When playing with WPF, I discovered the SizeToContent method, which enables me to create dynamically the content of a window, and then have it automatically resized to fit its content.
I try to do the same thing using WinForms, and it fails.
I used the AutoSize and AutoSizeMode, but it fails to correctly size to its content.
Is there a way to create a WinForm with controls that adjust to the size of their content, as in WPF?
Thanks,
As far as I remember this is a bit tricky in Windows Forms. Basically you need to use auto-sizing containers (e.g. TableLayoutPanel, etc. – and set their AutoSize
property to true
) in the form exclusively. Laying out controls with Anchor
and Dock
does not work.
来源:https://stackoverflow.com/questions/5310666/sizetocontent-equivalent-for-a-winform