I have a Listbox in a SplitterPanel. I have overriden it's MeasureItem() and DrawItem() methods.
What I want to do is, depending on Listbox.Width, return either the entire string or a shortened version of it, like "Dance toni...".
I've browsed SO and found two questions that pertain to my problem. One of the problems is measuring the width of the text, which I am doing with e.Graphics.MeasureString() in DrawItem().
Summary - I have the width of a listbox, and the width, in pixels, of a string. If the string is shorter than the width of a listbox, I want to display the string in entirety. However, if it's longer, I would like to return a version like "Hello every..." that would fit in within the width of the listbox.
So far I have:
private string FitText(string s)
{
int width = (TextRenderer.MeasureText(s, titleFont)).Width;
if (width <= mailList.Width)
{
return s;
}
else if (width > mailList.Width)
{
// What goes here?
}
}
I'm pretty sure it's just simple math, but I still can't figure it out.