.net – How to make a form resize vertically and horizontally when a control grows

layoutnetresizewinforms

(WinForms .net 2.0)

I'm making a form which, much like the standard MessageBox, has a single label on it and should grow horizontally and vertically if the text on the label grows. Horizontal seems easy enough, by setting AutoSize = true on the label and the form. However, I also want to make the label grow vertically, ie if it has newlines, but this doesn't seem so straightforward. I'm sure it is, but what settings are needed so that the label and form will grow vertically? I've got my label set to Anchor = top, left, right, bottom, so I'd have thought that when text with newlines is set in the label the height of the label will increase, and since Anchor = top & bottom it will force the form to increase in height. This doesn't seem to happen. What am I missing?

Best Answer

set Autosize=True and AutoSizeMode=GrowAndShrink on the form. Also make sure your controls will auto size to their content, and don't anchor to all four sides.

Related Topic