86

Is there a way to do a word wrap in a .NET label control?

I know there is an alternate way of using a TextBox, make property BorderStyle to none, property ReadOnly to true and set property WordWrap and property Multiline to true.

Is there something for a label?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sharpeye500
  • 8,775
  • 25
  • 95
  • 143

7 Answers7

239

Change your maximum size,

label1.MaximumSize = new Size(100, 0);

And set your autosize to true.

label1.AutoSize = true;

That's it!

fa wildchild
  • 2,557
  • 2
  • 15
  • 12
52

Just set Label AutoSize property to False. Then the text will be wrapped and you can re-size the control manually to show the text.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
25

Refer to Automatically Wrap Text in Label. It describes how to create your own growing label.

Here is the full source taken from the above reference:

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();
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mindandmedia
  • 6,800
  • 1
  • 24
  • 33
  • I am trying to add a new property to existing label, like AutoSize, something like IsWordWrap=true. Any thoughts? so that i can give LabelName.IsWordWrap=true; – Sharpeye500 Mar 01 '12 at 01:22
  • TextRenderer.MeasureText in my case gave wrong size. To make everything perfect you need also override OnPaint and draw text like this: `TextRenderer.DrawText(e.Graphics, this.Text, this.Font, new Rectangle(0, 0, this.Width, Int32.MaxValue), ForeColor, TextFormatFlags.WordBreak);` – user1561713 Nov 09 '16 at 14:20
21

Ironically, turning off AutoSize by setting it to false allowed me to get the label control dimensions to size it both vertically and horizontally which effectively allows word-wrapping to occur.

atconway
  • 20,624
  • 30
  • 159
  • 229
14

If you open the dropdown for the Text property in Visual Studio, you can use the enter key to split lines. This will obviously only work for static text unless you know the maximum dimensions of dynamic text.

Jurgen Camilleri
  • 3,559
  • 20
  • 45
5

If you want some dynamic sizing in conjunction with a word-wrapping label you can do the following:

  1. Put the label inside a panel
  2. Handle the ClientSizeChanged event for the panel, making the label fill the space:

    private void Panel2_ClientSizeChanged(object sender, EventArgs e)
    {
        label1.MaximumSize = new Size((sender as Control).ClientSize.Width - label1.Left, 10000);
    }
    
  3. Set Auto-Size for the label to true

  4. Set Dock for the label to Fill
noelicus
  • 14,468
  • 3
  • 92
  • 111
-5

You can use a TextBox and set multiline to true and canEdit to false .

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
mLar
  • 2,967
  • 2
  • 22
  • 23