Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

indicate truncation in ToolTipStatusLabel automatically

Tags:

c#

.net

winforms

I have a .NET application with a StatusStrip holding three ToolTipStatusLabels. The Text of the labels are filled from the application as they show the status. For some circumstances they can hold an empty text.

When I resize the window, the ToolTipStatusLabels are hidden when they cannot be fit in the StatusStrip. I would like to have the text truncated when the label cannot be fit in the StatusStrip. The default behavior to hide the label makes it difficult to distinguish between empty text or hidden label.

To indicate that the text is truncated automatically, this should be indicated with an ellipsis (...). How can this be done?

like image 660
harper Avatar asked May 25 '10 08:05

harper


1 Answers

Set the label's Spring property to True to get is to adjust its size automatically. To get ellipses you'll need to override the painting. Add a new class to your project and paste the code shown below. Compile. You'll get the new SpringLabel control in the status strip designer dropdown list.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;

[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.StatusStrip)]
public class SpringLabel : ToolStripStatusLabel {
    public SpringLabel() {
        this.Spring = true;
    }
    protected override void OnPaint(PaintEventArgs e) {
        var flags = TextFormatFlags.Left | TextFormatFlags.EndEllipsis;
        var bounds = new Rectangle(0, 0, this.Bounds.Width, this.Bounds.Height);
        TextRenderer.DrawText(e.Graphics, this.Text, this.Font, bounds, this.ForeColor, flags);
    }
}

You'll need to do more work if you use the Image or TextAlign properties.

like image 57
Hans Passant Avatar answered Sep 22 '22 01:09

Hans Passant