Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

c# - indicate truncation in ToolTipStatusLabel automatically

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...