Home > Mobile >  C# winforms label.size not truncating label text
C# winforms label.size not truncating label text

Time:09-21

I have label which contains text obtained from the user's local machine -- edit - and text length is neither fixed nor predictable. It was added to the form using VS designer and is AutoSize = true. If the width of the label exceeds 260 I want to switch to fixed width/AutoEllipse. When the label width exceeds the max limit, however, instead of truncating the text, the text continues to the edge of the form (and probably beyond that, actually). From what I've read the MaximumSize, AutoSize and AutoEllipse don't work together. Code I'm using:

        string maxDesc = "Lorem ipsum dolor sit amet, consectetur adipiscing";
        int destW;
        int destWmax;

        DestNameLBL.Text = maxDesc;
        destW = DestNameLBL.Width;
        if (destW >= 260)
        {
            destWmax = 260;
            DestNameLBL.Size = new Size(destWmax, 16);
            DestNameLBL.AutoEllipsis = true;
        }
        else
        {
            DestNameLBL.AutoSize = true;
            destWmax = destW;
        }

I can achieve my objective by creating a control programmatically:

        Label destDesc = new Label();
        destDesc.Location = new Point(100, 119);
        destDesc.Text = maxDesc;
        destDesc.Font = new Font("Microsoft Sans Serif", 9.75F, FontStyle.Regular);
        if (destW >= 260)
        {
            destWmax = 260;
            destDesc.Size = new Size(destWmax, 16);
            destDesc.AutoEllipsis = true;
        }
        else
        {
            destDesc.AutoSize = true;
            destWmax = destW;
        }
        Controls.Add(destDesc);

The sets of code are essentially identical. Why is this working with a programmatically created control but not with the control added via the designer (or is that even the relevant differentiation)?

CodePudding user response:

[Based on multiple comments above.]

A label placed on a form in VS designer with AutoSize=true does not have the AutoSize automatically overridden when programmatically setting the control to a fixed size.

It must be explicitly overridden.

AutoSize default for controls added in designer is true but for controls defined "in code" the default is false. Therefore identical code may behave differently depending on how the control is added.

Using MaximumSize and AutoEllipsis together will truncate display of the text with the ellipsis BUT (in my testing) will not show the ToolTip containing the complete text on MouseOver.

This code is working for truncating with ellipsis and ToolTip

        string maxDesc = "Lorem ipsum dolor sit amet, consectetur adipiscing elit";
        int destW;
        int destWmax;

        DestNameLBL.Text = maxDesc;
        destW = DestNameLBL.Width;
        if (destW >= 260)
        {
            destWmax = 260;
            DestNameLBL.AutoSize = false;
            DestNameLBL.Size = new Size(destWmax, 16);
            DestNameLBL.AutoEllipsis = true;
        }
        else
        {
            DestNameLBL.AutoSize = true;
            destWmax = destW;
        }
  • Related