Word wrap for a label in Windows Forms

190,672

Solution 1

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.

Solution 2

Actually, the accepted answer is unnecessarily complicated.

If you set the label to AutoSize, it will automatically grow with whatever text you put in it. (This includes vertical growth.)

If you want to make it word wrap at a particular width, you can set the MaximumSize property.

myLabel.MaximumSize = new Size(100, 0);
myLabel.AutoSize = true;

Tested and works.

Solution 3

In my case (label on a panel) I set label.AutoSize = false and label.Dock = Fill. And the label text is wrapped automatically.

Solution 4

There is no autowrap property but this can be done programmatically to size it dynamically. Here is one solution:

  • Select the properties of the label

  • AutoSize = True

  • MaximumSize = (Width, Height) where Width = max size you want the label to be and Height = how many pixels you want it to wrap

    Sample Properties

Solution 5

From MSDN, Automatically Wrap Text in Label:

using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

public class GrowLabel : Label {
    private bool mGrowing;
    public GrowLabel() {
        this.AutoSize = false;
    }
    private void resizeLabel() {
        if (mGrowing) 
            return;
        try {
            mGrowing = true;
            Size sz = new Size(this.Width, Int32.MaxValue);
            sz = TextRenderer.MeasureText(this.Text, this.Font, sz, TextFormatFlags.WordBreak);
            this.Height = sz.Height;
        }
        finally {
            mGrowing = false;
        }
    }
    protected override void OnTextChanged(EventArgs e) {
        base.OnTextChanged(e);
        resizeLabel();
    }
    protected override void OnFontChanged(EventArgs e) {
        base.OnFontChanged(e);
        resizeLabel();
    }
    protected override void OnSizeChanged(EventArgs e) {
        base.OnSizeChanged(e);
        resizeLabel();
    }
}
Share:
190,672

Related videos on Youtube

Nagu
Author by

Nagu

Updated on May 14, 2022

Comments

  • Nagu
    Nagu almost 2 years

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

  • Ohad Schneider
    Ohad Schneider about 14 years
    In order to break on characters rather than words (useful when you have long strings without spaces such as file paths), use (TextFormatFlags.WordBreak | TextFormatFlags.TextBoxControl) instead. See the last post in the same MSDN thread.
  • TheBlastOne
    TheBlastOne almost 13 years
    If you have the label snap into it's container, you can switch off AutoSize, leave the max size property as it is, and it will word-wrap exactly as we want. KISS at work!
  • madeFromCode
    madeFromCode almost 12 years
    Select the label you're dynamically adding text to. Look at the properties for the label and turn off AutoSize. You will now be able to drag/set the area for the label and it will automatically auto-wrap to stay within those parameters. No need for any additional coding.
  • Robin
    Robin over 11 years
    You can not achieve a fluid width with that solution.
  • dhara tcrails
    dhara tcrails over 11 years
    @Sam: Yes you can, you just set the proper "anchor" properties. Have you even tried it?
  • Robin
    Robin over 11 years
    @JohnGietzen I did. Probably we are not talking about the same. I've a label and a panel below this label. Everything should be fluid in width. I want that the panels top is always positioned on the labels bottom. I
  • dhara tcrails
    dhara tcrails over 11 years
    Sam: you should be able to set the Dock property of both the label and panel to Top, instead of my solution.
  • Chris Morgan
    Chris Morgan over 11 years
    I have latched on to OnResize in the parent and call myLabel.MaximumSize = new Size(Bounds.Width, 0);
  • theJerm
    theJerm about 11 years
    Voting this down is fine, but at least say why you did it. The reason this was voted down is because an <asp:Label> is not used in WinForms. This answer is for the wrong platform.
  • Stealth Rabbi
    Stealth Rabbi over 10 years
    Are steps 3 and 4 done in the designer? Any reason why they are step 3 and 4?
  • noelicus
    noelicus over 10 years
    They are done in the designer (but could be done in code if you need it to) and there's no reason for the order.
  • Peter Mortensen
    Peter Mortensen over 10 years
    This makes the label fill the entire form and make it obscure all other controls in the form... That is, it does not seem to work. Are there other requirements such that it will not fill the entire form?
  • alex555
    alex555 over 10 years
    the label is on a panel, not on the form directly. Therefore the text does not exceed the bounds of the panel
  • Peter Mortensen
    Peter Mortensen over 10 years
    OK, but then wouldn't it obscure all other controls in the panel?
  • alex555
    alex555 over 10 years
    What prevents you from creating an extra panel containing only the label?
  • Trikaldarshiii
    Trikaldarshiii about 10 years
    because it is asp.net not winform
  • palehorse
    palehorse almost 10 years
    One more note, if you leave the maximum height at 0, it will continue to expand vertically.
  • Igor Brejc
    Igor Brejc over 9 years
    Also, don't forget to include label's top and bottom padding in the calculation (Height = sz.Height + Padding.Vertical;)
  • Bengi Besçeli
    Bengi Besçeli about 9 years
    My label doen't have the AutoSize and MaximumSize properties, how can I add these ?
  • Bengi Besçeli
    Bengi Besçeli about 9 years
    My label doen't have the AutoSize and MaximumSize properties, how can I add these properties ?
  • ygoe
    ygoe over 8 years
    Doesn't work. The Label increases the width of my panel just that it doesn't have to wrap. Unfortunately, the window is not as wide.
  • PahJoker
    PahJoker over 8 years
    This is a good solution, but I might prefer using ReadOnly to setting Enabled to false.
  • Tim Coulter
    Tim Coulter almost 8 years
    This is an imaginative solution that also works well on Compact Framework (where Label does not have AutoSize and MaximumSize properties).
  • Philip Brack
    Philip Brack over 6 years
    This is precisely what I was looking for as it pertains to formatting my form with explanatory text that is in a paragraph form.
  • user854301
    user854301 almost 6 years
    This is working to great effect for me in combination with a TableLayoutPanel and resetting the MaximumSize on Resize of the parent container with a combination of AutoSize and Percentage rows. It seems like the simplest solution for auto-wrapping width-filling auto-height labels.
  • Edwin
    Edwin over 5 years
    That seems like some extra layout that should be unnecessary. Surely the label should have its own box rather than needing to be put into another box.
  • Edwin
    Edwin over 5 years
    @madeFromCode You'd need to set the height programmatically if you want the label to expand vertically else your text will be clipped.
  • EdSF
    EdSF about 4 years
    @Edwin - just fyi "Surely the label should have its own box rather than needing to be put into another box" - in other words, a container, which is what a Panel is (among other containers).