简体   繁体   English

当文本太长时,StatusStrip标签不可见

[英]StatusStrip label not visible when text too long

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. 我有一个停靠在C#Form底部的StatusStrip ,它包含一个标签,其中的文本显示正常,除非文本长度较长,然后根本不显示,我必须加宽表单然后它突然出现了。 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. 因此,用户知道应用程序正在显示某些内容然后他可以自己扩展它,因为当它根本不可见时,它不向用户指示任何内容。

You can create a custom renderer based on ToolStripProfessionalRenderer and override OnRenderItemText method and draw text with ellipsis: 您可以基于ToolStripProfessionalRenderer创建自定义渲染器并覆盖OnRenderItemText方法并使用省略号绘制文本:

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: 然后,它足以使Renderer你的StatusStrip您的自定义渲染器:

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 : 在下面的示例中,您可以看到ToolStripStatusLabel的行为,它的Spring属性设置为true ,其StatusStrip使用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. 在Visual Studio 2017上,接受的答案对我不起作用。 So here is another simple solution. 这是另一个简单的解决方案。 Set LayoutStyle property of StatusStrip to Flow . 将StatusStrip的LayoutStyle属性设置为Flow ie: 即:

 statusStrip1.LayoutStyle= LayoutStyle.Flow;

And Set 并设置

`statusStrip1.AutoSize= false;`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM