StatusStrip label not visible when text too long

半城伤御伤魂 提交于 2020-01-01 08:50:21

问题


I have a StatusStrip docked to the bottom of a C# Form, it contains a label, the text in it displays fine, except when there is longer length of text then it does not display at all, and I have to widen the form and then all of a sudden it appears. Is it possible to show it in the form below:

    This is a very long tex...

So that the user knows that the app is showing something and then he can widen it himself, because when it is not visible at all, it does not indicate anything to user.


回答1:


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:




回答2:


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.




回答3:


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;`


来源:https://stackoverflow.com/questions/38155313/statusstrip-label-not-visible-when-text-too-long

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