5

How should I set WordWrap = false to a System.Windows.Forms.Label?

I have a header on a panel, and it should show "MyPanel capt...". So I use AutoEllipsis = true, but it is not sufficient.

I also use "AutoSize = true", because I want that the label takes the minimum space possible.

Apropos, Visual Basic 6.0 did it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
serhio
  • 28,010
  • 62
  • 221
  • 374
  • 1
    What visual effect are you trying for? – Jay Riggs Apr 13 '10 at 14:56
  • I had a similar problem, did't find a satisfactory solution. You may want to check out my answer [here](https://stackoverflow.com/questions/13935275/prevent-word-wrap-in-label-with-autosize-false/53016161#53016161). – shash Oct 26 '18 at 20:59

7 Answers7

8

I've got a similar effect working using:

label1.AutoSize = false;
label1.AutoEllipsis = true;

and sizing the label area to be one line in height only.

Neil Moss
  • 6,598
  • 2
  • 26
  • 42
1

I'm pretty sure you can't prevent labels from wrapping. An alternate (if slightly crude) option is to set the label to auto-size (so the width grows with the text), and then put a Control next to it that sits in front of it in the z-order. That way, when the label width goes past a certain point the content of the label overlap will be hidden by that other control.

Like I said, it is a pretty crude method of achieving the effect.

Also, if you are trying to use AutoEllipsis, i'm assuming you've disabled AutoResize? I believe it takes precedence.

Alistair Evans
  • 36,057
  • 7
  • 42
  • 54
1

I found a solution:

this.label.AutoEllipsis = true;
this.label.AutoSize = true;

In the panel's event handler for Resize:

...
textHeight = this.label.Font.SizeInPoints; // Take in pixels, not points
...
Size newMaxSize = new Size(this.Width,
    textHeight + label.Padding.Top + label.Padding.Bottom);
this.label.MaximumSize = newMaxSize;
...
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
serhio
  • 28,010
  • 62
  • 221
  • 374
1

Try setting the labels MaximumSize Property.

Tester101
  • 8,042
  • 13
  • 55
  • 78
0

I don't think there's any way to do this - labels aren't single- or multi-line, per se. It just depends on whether the Text property of the label has any line breaks in it.

If you want it to be single line, replace the CRLF characters in the Text with something else.

SqlRyan
  • 33,116
  • 33
  • 114
  • 199
  • If I have a long phrase without line breaks it passes to the new line. see the explications in update. – serhio Apr 13 '10 at 15:09
  • The reverse: *[Label word wrapping](http://stackoverflow.com/questions/9509147/label-word-wrapping/10069986#10069986)* – Peter Mortensen May 10 '16 at 09:46
  • That is not true. I have no return or line feed characters, yet it is still autowrapping to become multi-lined – Bernoulli Lizard Oct 07 '20 at 15:54
  • 1
    @BernoulliLizard This may be true now - I answered this question 10 years ago and if it's different now and auto-wrapping, that may have been a feature that was added to .NET since then. – SqlRyan Nov 18 '20 at 20:41
0

Ah, I think I finally understand the effect you want.

You want a label that will AutoSize up to a maximum amount. After the maximum, you want to show AutoEllipsis. Correct?

If so, then you need to set the MaximumSize, AutoEllipsis, and AutoSize properties. Then the label will be as small as possible. When the text exceeds the maximum size you specified, the text will be truncated and an ellipsis appended. You do not need code to do this.

AMissico
  • 21,470
  • 7
  • 78
  • 106
  • 1
    Yes; you understood correctly. But the problem is setting MaxSize. parent panel is resiezible, so see my answer for the trick. – serhio Apr 13 '10 at 15:37
0

I'm using a FlowLayoutPanel to hold the labels with left to right flow. So autosize and overlap breaks my nicely aligned columns. I think the most direct way is to just implement paint yourself. Helpers exist to do the ellipsis for you.

That last TextFormatFlags has a dozen options which save you tons of annoying drawing code.

    private void templateLabel_Paint(object sender, PaintEventArgs e)
    {
        Label lbl = sender as Label;
        e.Graphics.Clear(lbl.BackColor);

        TextRenderer.DrawText(e.Graphics, lbl.Text, lbl.Font,
            lbl.ClientRectangle,
            Color.Black,
            lbl.BackColor, TextFormatFlags.EndEllipsis);
    }
user922020
  • 712
  • 6
  • 12