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);
}
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.
Use Graphics.DrawString method (TextRenderer.DrawText is a GDI way, Graphics.DrawString - GDI+). Set StringFormat.Trimming property to StringTrimming.EllipsisCharacter (EllipsisWord, EllipsisPath).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With