5

I've got a statusstrip with a number of items. One of them is a ToolStripStatusLabel with Spring = True. When the text of the label is too long, one can't see it.

Is it possible to make the statusstrip become higher and show whole text in multiline?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
horgh
  • 17,918
  • 22
  • 68
  • 123
  • 1
    ToolStripStatusLabel doesn't support rendering text in multiple lines. You'd have to create your own derived class. This is *not* easy to get right, ToolStripItem is difficult to deal with and the auto-sizing gets awfully complicated. – Hans Passant Jul 19 '12 at 11:59
  • You can Measure the string of label (its text) in size, and if it is bigger than width of the control increase the height as needed. [Check here](http://msdn.microsoft.com/en-us/library/aa327655%28v=vs.71%29.aspx) – Dumbo Jul 19 '12 at 10:08
  • I think in this solution the issue will be making the ToolStripStatusLabel multiline...may be we will need to paint the label ourselves and drawString the text in two lines... – Arif Eqbal Jul 19 '12 at 10:59

1 Answers1

6

This is an interesting problem....I tried a couple of things but no success...basicall the ToolStripStatusLabel is very limited in capability.

I ended up trying a hack that gives the result you want but am not sure even I would recommend this unless of course this is absolutely necessary...

Here's what I have got...

In the properties of your StatusStrip set AutoSize = false, this is to allow the StatusStrip to be resized to accommodate multiple lines. I am assuming statusStrip called ststusStrip1 containing label called toolStripStatusLabel1.

At form Level declare a variable of TextBox type:

  TextBox txtDummy = new TextBox();

At Form Load set some of its properties:

  txtDummy.Multiline = true;
  txtDummy.WordWrap = true;
  txtDummy.Font = toolStripStatusLabel1.Font;//Same font as Label

Handle the paint event of the toolStripStatusLabel1

 private void toolStripStatusLabel1_Paint(object sender, PaintEventArgs e)
 {        

    String textToPaint = toolStripStatusLabel1.Tag.ToString(); //We take the string to print from Tag
    SizeF stringSize = e.Graphics.MeasureString(textToPaint, toolStripStatusLabel1.Font);
    if (stringSize.Width > toolStripStatusLabel1.Width)//If the size is large we need to find out how many lines it will take
    {
        //We use a textBox to find out the number of lines this text should be broken into
        txtDummy.Width = toolStripStatusLabel1.Width - 10;
        txtDummy.Text = textToPaint;
        int linesRequired = txtDummy.GetLineFromCharIndex(textToPaint.Length - 1) + 1;
        statusStrip1.Height =((int)stringSize.Height * linesRequired) + 5;
        toolStripStatusLabel1.Text = "";
        e.Graphics.DrawString(textToPaint, toolStripStatusLabel1.Font, new SolidBrush( toolStripStatusLabel1.ForeColor), new RectangleF( new PointF(0, 0), new SizeF(toolStripStatusLabel1.Width, toolStripStatusLabel1.Height)));
    }
    else
    {
        toolStripStatusLabel1.Text = textToPaint;
    }
} 

IMP: Do not assign the text property of your label instead put it in Tag we would use it from Tag

 toolStripStatusLabel1.Tag = "My very long String";

Screenshot

Arif Eqbal
  • 3,068
  • 1
  • 18
  • 10
  • There is only one thing I had to change in your code. As I wanted the ToolStrip to shrink back, if some new text is smaller than the former, I deleted the check of the string width and checked if the Tag property is set to null (to return, if so). So now the ToolStrip changes its height to and fro! Another thanks to you! – horgh Jul 20 '12 at 05:32