Oztaco
Oztaco

Reputation: 3459

How to figure out where to put "..." if text is bigger than container

Let's say I have a Label inside a Panel. The text is going to be bigger than the Panel sometimes, but not always. How would I figure out what part of the text I should at "..." in front of without hard-coding exactly how many characters it would take, because each character isnt the same size.

if (bigLabel.Width >= this.ClientRectangle.Width - 10) {
    dotLabel.Location = new Point(this.ClientRectangle.Width - 10 - dotLabel.Width);
}
else {
    dotLabel.Location = new Point(this.Width + 10, this.Height + 10);
}

Upvotes: 3

Views: 2281

Answers (3)

Stack Overflow
Stack Overflow

Reputation: 2794

If you use Telerik control - RadLabel, Only set this properties:

this.lblReferralTracking.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
this.lblReferralTracking.TextWrap = false;
this.lblReferralTracking.UseCompatibleTextRendering = true;

Upvotes: 0

Dennis
Dennis

Reputation: 37780

Use Graphics.DrawString method (TextRenderer.DrawText is a GDI way, Graphics.DrawString - GDI+). Set StringFormat.Trimming property to StringTrimming.EllipsisCharacter (EllipsisWord, EllipsisPath).

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941990

Leave it up to TextRenderer.DrawText() to figure that out itself. Specify the TextFormatFlags.EndEllipsis option. You'll find a code sample in this answer.

Which is already built in to the Label control. Set its AutoSize property to False and AutoEllipis property to True to have it all done automatically. And you get a tooltip for free that shows the missing text.

Upvotes: 8

Related Questions