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.
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;`
来源:https://stackoverflow.com/questions/38155313/statusstrip-label-not-visible-when-text-too-long