tags:

views:

92

answers:

6

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

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

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

A propos, VB6 did it.

A: 

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.

rwmnau
If I have a long phrase without line breaks it passes to the new line. see the explications in update.
serhio
A: 

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.

Kazar
No, I use both. AutoEllipsis for the maximum size. Autosize for minimum place.
serhio
Ah, ok, well I think AutoSize will just increase the size of the label to the point that AutoEllipsis is no longer needed.
Kazar
AutoEllipsis is needed when the label atteints the parent width. see my answer for the trick.
serhio
A: 

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
I use also "AutoSize = true", because want that the label take minimum space possible.
serhio
A: 

Find a solution.

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

In the panel OnRezise()

...
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;
...
serhio
A: 

Try setting the labels MaximumSize Property.

Tester101
A: 

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
Yes; you understood correctly. But the problem is setting MaxSize. parent panel is resiezible, so see my answer for the trick.
serhio
:O) Man, you killing us. Need to be more specific in your question. You only give out pieces of the puzzle, little by little. Put all pieces into question. Then we can help you quicker.
AMissico
All the pieces is the Universe, man :)
serhio
.Well said. :O)
AMissico