How To Auto Ellipse Text in C#
By FoxLearn 1/16/2025 3:58:37 AM 25
One effective way to address this is by using an ellipsis (...
) to indicate that the text has been truncated.
Use the AutoEllipsis Property on a Label
The simplest way to implement text ellipsing in C# is by using the AutoEllipsis
property of a Label
. This method is particularly useful when you're working with standard UI controls and want a quick solution without additional complexity.
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";
In this example, if the text is too long to fit within the defined size of the label, the text will automatically be truncated and an ellipsis (...
) will be added at the end.
Using the TextRenderer Class for Custom Rendering
What if you are not using labels or you need more control over text rendering? C# offers a more flexible solution through the TextRenderer
class. This class provides various methods for drawing and measuring strings, and it’s the underlying technology used by Label
controls to render text, including handling text ellipsing.
To use the TextRenderer
class, you'll utilize the DrawText
method, which allows you to draw text directly on a form, applying ellipsing and other formatting options.
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); }
When the text overflows the defined area (100px in width), it will be truncated, and the ellipsis will be added at the end.
This form allows you to type text and adjust the width of the bounding box to see how the text is automatically ellipsed.
using System; using System.Drawing; using System.Windows.Forms; namespace EllipseApplication { public class EllipseForm : Form { private TextBox sourceTextBox = new TextBox(); private NumericUpDown widthBox = new NumericUpDown(); private Label textboxLabel = new Label(); private Label widthBoxLabel = new Label(); private Font textFont = new Font("Arial", 11); public EllipseForm() { this.SuspendLayout(); this.Text = "Ellipse Test"; this.Size = new Size(300, 300); this.sourceTextBox.Location = new System.Drawing.Point(125, 12); this.sourceTextBox.Size = new System.Drawing.Size(155, 20); this.sourceTextBox.TextChanged += delegate(object sender, EventArgs e) { this.Invalidate(); }; this.Controls.Add(this.sourceTextBox); this.widthBox.Location = new System.Drawing.Point(125, 38); this.widthBox.Size = new System.Drawing.Size(55, 20); this.widthBox.Value = 50; this.widthBox.Maximum = 300; this.widthBox.ValueChanged += delegate(object sender, EventArgs e) { this.Invalidate(); }; this.Controls.Add(this.widthBox); this.textboxLabel.Location = new System.Drawing.Point(10, 12); this.textboxLabel.Size = new System.Drawing.Size(105, 20); this.textboxLabel.Text = "Text To Ellipse:"; this.textboxLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight; this.Controls.Add(this.textboxLabel); this.widthBoxLabel.Location = new System.Drawing.Point(10, 38); this.widthBoxLabel.Size = new System.Drawing.Size(105, 20); this.widthBoxLabel.Text = "Width of Final String:"; this.widthBoxLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight; this.Controls.Add(this.widthBoxLabel); this.ResumeLayout(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); TextRenderer.DrawText(e.Graphics, this.sourceTextBox.Text, textFont, new Rectangle(10, 75, (int)this.widthBox.Value, 50), Color.Black, Color.White, TextFormatFlags.EndEllipsis); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new EllipseForm()); } } }
Ellipsing text in C# is straightforward with either the AutoEllipsis
property on Label
controls or by using the TextRenderer
class for custom text rendering.
- How to fix 'Failure sending mail' in C#
- How to Parse a Comma-Separated String from App.config in C#
- How to convert a dictionary to a list in C#
- How to retrieve the Executable Path in C#
- How to validate an IP address in C#
- How to retrieve the Downloads Directory Path in C#
- C# Tutorial
- Dictionary with multiple values per key in C#