1

I have a label with AutoSize = false and AutoEllipsis = true and I noticed that in case when ellipsis added to the text of this label it moves a few pixels like in the following screenshots:

enter image description here

enter image description here

Why? What am I doing wrong? How can I fix it?

Thanks in advance.

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • possible duplicate of [AutoEllipsis=true affects the vertical positioning of the text](http://stackoverflow.com/questions/24447344/autoellipsis-true-affects-the-vertical-positioning-of-the-text) – TaW Mar 26 '15 at 08:41
  • You are not doings anything wrong, it is a bug. In contrast to the answer in the duplicate, the best solution is to write your own autoellispis code. Some projects are out there but all rather complicated.. – TaW Mar 26 '15 at 08:43
  • Do you know if there is a bug reported with MS for this? – russelrillema Oct 04 '22 at 11:26

3 Answers3

1

The simplest way to work around it is by setting the label's UseCompatibleTextRendering property to True.

Bhupendra
  • 350
  • 3
  • 16
1

This seems to be a bug.

Whenever the text is too large it aligns to the Top, with (2) or without (1) AutoEllipsis on.

enter image description here

Unfortunately the proposed solution often looks rather ugly.

To workaround (3) you can either use a simple function like the one below or write a Label subclass that integrates the functionality.. :

string ReducedText(Label lbl, string text)
{
    char ell = (char)0x00002026;

    lbl.Tag = text;
    string redString = text;
    using (Font font = new Font(lbl.Font, FontStyle.Bold))
    using (Graphics G = lbl.CreateGraphics())
    {
        G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        float elltWidth = TextRenderer.MeasureText(G, ell.ToString(), font).Width;
        float textWidth = TextRenderer.MeasureText(G, redString, font).Width;
        elltWidth += lbl.Padding.Left + lbl.Padding.Right ;
        if (textWidth < lbl.ClientSize.Width) return text;  // maybe watch padding!
        SizeF s = SizeF.Empty;
        do
        {
            redString = redString.Substring(0, redString.Length - 1);
            s = TextRenderer.MeasureText(G, redString, font);
            // Console.WriteLine(s.Width + "  " + lbl.ClientSize.Width + " " + redString);
        } while (s.Width + elltWidth > lbl.ClientSize.Width);
    }
    return redString + ell;
}

On some bold fonts the ellipsis character looks like an underscore; in that case simply replace it with a string with 2-3 dots..!

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
  • Unfortunately your solution doesn't seem to work in my case -- text in the label still aligns. I use it like this -- `this.label.Text = ReducedText(this.label, str);` – FrozenHeart Mar 26 '15 at 09:47
  • 1
    Hm, the usage is as intended. Assuming that the TextAlignment is MiddleLeft you may need to increase the slack (3) I had to add a little; try 5-8.. – TaW Mar 26 '15 at 10:03
  • 1
    Ok. Can you explain the Label a little: What are the images shown there? – TaW Mar 26 '15 at 10:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73834/discussion-between-taw-and-frozenheart). – TaW Mar 26 '15 at 10:18
  • It's a simple Label with the following settings -- http://i.imgur.com/DtJEw6c.png, http://i.imgur.com/Rz01tTf.png – FrozenHeart Mar 26 '15 at 10:19
0

Setting the top and bottom of the label's margin property to 3 seemed to do it for me. So set you margin to (3,3,3,3)!

Nolemonpledge
  • 139
  • 1
  • 2
  • 11