tags:

views:

112

answers:

1

The TextRenderer.DrawText method has a TextFormatFlags parameter. Using TextFormatFlags.EndEllipsis allows you to abbreviate the text with an ellipsis at the end so that it fits in the available space. However, I want to put the ellipsis at the start. Curiously enough there is no TextFormatFlags value to do this.

I've considered progressively shortening the string myself until it fits according to TextRenderer.MeasureText, but I'm worried about performance. Surely there is a better way?

+2  A: 

Rather than shortening, you should lengthen in "binary-search" fasion. Since a string could be considerably longer than the visible area, if you start with one character, and grow by a factor of 2 each iteration, then shrink by a few characters at a time once your behond your length limit, you should have a performant truncation algorithm.

jrista
This is what I have implemented now, except that I use a full dichotomic search (instead of "shrinking by a few characters at a time"). Good tip on the lengthening, I hadn't considered the "huge string" case.
Wim Coenen