Word wrap for a label in Windows Forms
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:
- Measure the height of the text (Graphics.MeasureString).
- If the label height is not equal to the height of the text set the height and return.
- 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
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();
}
}
Related videos on Youtube
Nagu
Updated on May 14, 2022Comments
-
Nagu almost 2 years
How can one get word wrap functionality for a
Label
for text which goes out of bounds? -
Ohad Schneider about 14 yearsIn 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 almost 13 yearsIf 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 almost 12 yearsSelect 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 over 11 yearsYou can not achieve a fluid width with that solution.
-
dhara tcrails over 11 years@Sam: Yes you can, you just set the proper "anchor" properties. Have you even tried it?
-
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 over 11 yearsSam: you should be able to set the
Dock
property of both the label and panel toTop
, instead of my solution. -
Chris Morgan over 11 yearsI have latched on to
OnResize
in the parent and callmyLabel.MaximumSize = new Size(Bounds.Width, 0);
-
theJerm about 11 yearsVoting 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 over 10 yearsAre steps 3 and 4 done in the designer? Any reason why they are step 3 and 4?
-
noelicus over 10 yearsThey 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 over 10 yearsThis 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 over 10 yearsthe label is on a panel, not on the form directly. Therefore the text does not exceed the bounds of the panel
-
Peter Mortensen over 10 yearsOK, but then wouldn't it obscure all other controls in the panel?
-
alex555 over 10 yearsWhat prevents you from creating an extra panel containing only the label?
-
Trikaldarshiii about 10 yearsbecause it is asp.net not winform
-
palehorse almost 10 yearsOne more note, if you leave the maximum height at 0, it will continue to expand vertically.
-
Igor Brejc over 9 yearsAlso, don't forget to include label's top and bottom padding in the calculation (
Height = sz.Height + Padding.Vertical;
) -
Bengi Besçeli about 9 yearsMy label doen't have the AutoSize and MaximumSize properties, how can I add these ?
-
Bengi Besçeli about 9 yearsMy label doen't have the AutoSize and MaximumSize properties, how can I add these properties ?
-
ygoe over 8 yearsDoesn'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 over 8 yearsThis is a good solution, but I might prefer using ReadOnly to setting Enabled to false.
-
Tim Coulter almost 8 yearsThis is an imaginative solution that also works well on Compact Framework (where Label does not have AutoSize and MaximumSize properties).
-
Philip Brack over 6 yearsThis is precisely what I was looking for as it pertains to formatting my form with explanatory text that is in a paragraph form.
-
user854301 almost 6 yearsThis 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 over 5 yearsThat 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 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 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).