17

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.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Ahmed
  • 14,503
  • 22
  • 92
  • 150
  • 2
    Just measure the text width and trim it at the status labels width, then SubString() + "..."; – Jeremy Thompson Jul 02 '16 at 00:25
  • What kind of VS do you use ? I'm using VS2015 Update 3, .NET 4.0, Win10 and it's ok. Text will be truncated if Width of Form less then Width of Text. "...it does not display at all" is not happening on my form. –  Jul 02 '16 at 00:37
  • @JeremyThompson That's doable but when user extends form I have to refresh it again otherwise it will show incomplete text. – Ahmed Jul 02 '16 at 00:58
  • @x... I am using VS 2010 and .Net framework 3.5 – Ahmed Jul 02 '16 at 00:59

3 Answers3

18

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:

enter image description here

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • I'm using VS2015.3 , .NET 4.0, and Win10. That is the default behaviour from label in statusStrip. I dont need to extend it. –  Jul 02 '16 at 00:53
  • @x... The sample is created using `VS 2013.4` and `.NET 4.5` on `Windows 8.1`. It seems we need a fix. – Reza Aghaei Jul 02 '16 at 00:56
  • Thanks @RezaAghaei I am on VS 2010 and .Net 3.5 windows 8.1 – Ahmed Jul 02 '16 at 01:00
  • Thank for the great solution @RezaAghaei ! Please note that your answer should also include that this will only work if you have set `this.statusStrip1.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Table;` A LayoutStyle of `HorizontalStackWithOverflow` will NOT work. – Joe Uhren Mar 08 '20 at 00:24
8

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.

Ronny D'Hoore
  • 752
  • 7
  • 9
4

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;`
HN Learner
  • 544
  • 7
  • 19
  • 1
    Probably you have made a mistake applying the solution. Make sure you have added a status trip label. Also make sure you have set spring property of the label to true and you are using the renderer which I posted in answer. – Reza Aghaei Mar 31 '17 at 09:26