Home » » How To Auto Ellipse Text in C#

How To Auto Ellipse Text in C#

A while back we posted a tutorial on how to auto ellipse text in javascript, and it seems that people find it useful, because it gets a good amount of search traffic. One thing we did notice, however, is that a lot of people seem to end up there when they are actually searching for how to auto ellipse text in C#. And while you could implement the algorithm discussed on the javascript tutorial in C#, it is by no means the easiest way to ellipse strings in C#.

What is the best way? There are actually two possibilities, and which you should use depends on your situation. The first is extremely simple - you put text in a label, and you set that label's
AutoEllipsis property to true.
With AutoEllipsis set to
TrueWith AutoEllipsis set to
False

Label myLabel = new Label();
myLabel.Location = new System.Drawing.Point(10, 10);
myLabel.Size = new System.Drawing.Size(100, 15);
myLabel.AutoEllipsis = true;
myLabel.Text = "Some Text That Will Be Ellipsed";
As you can see, the Label will by default always truncate your text. But with AutoEllipsis on, it will put ellipses on the end of the string if it is being truncated.

So that's pretty simple. But what if you aren't using labels - your drawing directly on the screen? Well, there is a built in solution in C# for that as well. It is a little known class called TextRenderer which has a bunch of the logic for string rendering built right in. The TextRenderer class is actually what a Label uses to draw its contents, and to do its AutoEllipsing.

Using the TextRenderer class is extremely simple. It is comprised of a number of static methods for drawing and measuring strings. To use it for ellipsing, we will be using the following method:

public static void DrawText(IDeviceContext dc, string text, Font font,
    Rectangle bounds, Color foreColor, Color backColor, TextFormatFlags flags);
There are two arguments that may look unfamiliar to you: IDeviceContext dc and TextFormatFlags flags. The IDeviceContext in most cases will just be a Graphics object, which you will get from the arguments of an OnPaint method. The TextFormatFlags will be what we use to tell the TextRenderer to ellipse the string.

Here is a potential use of the method:

protected override void OnPaint(PaintEventArgs e)
{
  base.OnPaint(e);
  TextRenderer.DrawText(e.Graphics, "I am Some Text!", new Font("Arial", 11),
      new Rectangle(10, 75, 100, 50), Color.Black, Color.White,
      TextFormatFlags.EndEllipsis);
}
Here, we are overriding the OnPaint of some control, and painting the text "I am Some Text!" at position (10,75) with a width/height of (100,50) using the font Arial with a size of 11pt. The text will be black, and the background will be white, and if the text exceeds 100px long (the width specified in the bounds) it will be ellipsed so that it fits within 100px. It is that TextFormatFlags.EndEllipsis that causes the ellipsing magic to happen. There are a number of other flags as well, and you can check out the MSDN docs if you want to learn more about them.


0 comments:

Post a Comment

Popular Posts

Powered by Blogger.
.comment-content a {display: none;}