开发者

Set WordWrap = false for a Label

开发者 https://www.devze.com 2022-12-26 11:59 出处:网络
How should I set WordWrap = false to a Sys开发者_开发技巧tem.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 suffi

How should I set WordWrap = false to a Sys开发者_开发技巧tem.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.


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.


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.


Try setting the labels MaximumSize Property.


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;
...


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消