public String getSourceNameWrapping(String sourceName, Font font)
{
int gap = 25;
String[] words = sourceName.Split(' ');
int width = getRenderTextWidth(sourceName, listBox1, font);
String result = "";
if (width > listBox1.Width - gap)
{
result += words[0];
width = getRenderTextWidth(result, listBox1, font);
// check if the first word width is larger than the listbox1 width
if (width > listBox1.Width - gap)
{
while (getRenderTextWidth(result + " ...", listBox1, font) > listBox1.Width - gap)
{
result = result.Remove(result.Length - 1, 1);
}
}
else
{
for (int i = 1; i < words.Count(); i++)
{
String tempResult = result + " " + words[i];
width = getRenderTextWidth(tempResult + " ...", listBox1, font);
if (width > listBox1.Width - gap)
{
break;
}
else
{
result = tempResult;
}
}
}
result = result + " ...";
}
else
{
result = sourceName;
}
return result;
}