Label word wrapping

C#WinformsC# 4.0C# 3.0

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

Change your maximum size,

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

And set your autosize to true.

label1.AutoSize = true;

That's it!

Solution 2 - C#

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.

Solution 3 - C#

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();
  }
}

Solution 4 - C#

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.

Solution 5 - C#

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.

Solution 6 - C#

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

Solution 7 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSharpeye500View Question on Stackoverflow
Solution 1 - C#fa wildchildView Answer on Stackoverflow
Solution 2 - C#Abdullah AlhutamiView Answer on Stackoverflow
Solution 3 - C#mindandmediaView Answer on Stackoverflow
Solution 4 - C#atconwayView Answer on Stackoverflow
Solution 5 - C#Jurgen CamilleriView Answer on Stackoverflow
Solution 6 - C#noelicusView Answer on Stackoverflow
Solution 7 - C#mLarView Answer on Stackoverflow