I’m working on implementing a user control that changes its size depending on some properties. As I understand winforms layout engine asks each child control for its preferred size when it performs a layout and informs them about the maximum size that they can afford.
This is a description of GetPreferredSize in msdn:
Control.GetPreferredSize(Size proposedSize)Retrieves the size of a rectangular area into which a control can be fitted.
I’m confused by the following:
You can return a size larger than the
constraints indicated in the
proposedSize parameter, but
proposedSize should decrease as the
constraint decreases.
What does it mean? What will happen if I return the size larger than proposed?
Can somebody explain me how does this work?
What that line means is that you are free to return a larger preferred size than the
proposedSizeparameter, but thatproposedSizeshould still be affecting your preferred size. For example, your returned size forGetPreferredSize(new Size(100, 0)should be less than the returned size forGetPreferredSize(new Size(200, 0)).Note that nothing bad happens if you return a larger size; the layout engine will sort everything out for you, possibly by reducing the size available for another control. Ultimately, your preferred size is just a hint for the engine, so that it knows what the relative space demands are for the various UI components it is arranging.