Search code examples
c#.netwinformslabelcontrols

Word wrap for a label in Windows Forms


How can one get word wrap functionality for a Label for text which goes out of bounds?


Solution

  • The quick answer: switch off AutoSize.

    The big problem here is that the label will not change its height automatically (only width). To get this right you will need to subclass the label and include vertical resize logic.

    Basically what you need to do in OnPaint is:

    1. Measure the height of the text (Graphics.MeasureString).
    2. If the label height is not equal to the height of the text set the height and return.
    3. Draw the text.

    You will also need to set the ResizeRedraw style flag in the constructor.