How to truncate text

M

Mark B

I am using DrawString to write the value of a variable, Text1 (email
address), on a square.

If the text is greater than 200 pixels in length, I want to truncate the end
of the text and display three periods:

someones_long_email@somew...

Any help appreciated:

{
var lngText1Length = g.MeasureString(Text1, rectangleArialFont12).Width;
g.DrawString(Text1, rectangleArialFont12, SystemBrushes.WindowText, new
PointF(40, 218));
}
 
J

Jeff Johnson

If the text is greater than 200 pixels in length, I want to truncate the
end of the text and display three periods:

Take a look at the System.Drawing.StringFormat class.
 
R

RobinDotNet

Mark B said:
I am using DrawString to write the value of a variable, Text1 (email
address), on a square.

If the text is greater than 200 pixels in length, I want to truncate the
end of the text and display three periods:

someones_long_email@somew...

Any help appreciated:

{
var lngText1Length = g.MeasureString(Text1,
rectangleArialFont12).Width;
g.DrawString(Text1, rectangleArialFont12, SystemBrushes.WindowText, new
PointF(40, 218));
}

Here's an example:

StringFormat sf = new StringFormat();
sf.Trimming = StringTrimming.EllipsisCharacter;
Rectangle textRect =
new Rectangle(textPositionX, textPositionY, containerWidth,
containerHeight);
gfx.DrawString(thisIsMyText, thisIsMyFont, thisIsMyBrush, textRect, sf);

sf.Dispose();
gfx.Dispose();

//also don't forget to dispose of your font and brush
//gfx is the graphics instance I am using

RobinS.
GoldMail.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top