StatusStrip label not visible when text too long

为君一笑 提交于 2019-12-04 03:09:02

You can create a custom renderer based on ToolStripProfessionalRenderer and override OnRenderItemText method and draw text with ellipsis:

public class CustomRenderer : ToolStripProfessionalRenderer
{
    protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
    {
        if (e.Item is ToolStripStatusLabel)
            TextRenderer.DrawText(e.Graphics, e.Text, e.TextFont,
                e.TextRectangle, e.TextColor, Color.Transparent,
                e.TextFormat | TextFormatFlags.EndEllipsis);
        else
            base.OnRenderItemText(e);
    }
}

Then it's enough to set Renderer of your StatusStrip to your custom renderer:

this.statusStrip1.Renderer = new CustomRenderer();

In below example, You can see the behavior of a ToolStripStatusLabel which it's Spring property is set to true and its StatusStrip uses CustomRenderer:

If you set

ToolStripStatusLabel.Spring = True;

then you won't get the "..." but the text will be shown even when the available space is insufficient.

On Visual Studio 2017, the accepted answer didn't work for me. So here is another simple solution. Set LayoutStyle property of StatusStrip to Flow. i.e:

 statusStrip1.LayoutStyle= LayoutStyle.Flow;

And Set

`statusStrip1.AutoSize= false;`
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!