locked
DrawString and text wrapping? RRS feed

  • Question

  • Hi,

    I'm trying to use drawstring to print some text on a label but it is cutting off the text rather than wrapping it to the next line.  I have tried a few of the examples I could find online but none of them work and I'm hoping someone here can point me in the right direction.

    Here's my code so far:

    myFont = new Font("Arial", 10);

    SizeF mySize = new SizeF();

    mySize = e.Graphics.MeasureString(myString, myFont);

    mySize.Width = 480;                 // The width of the label stock

    Rectangle myRectangle = new Rectangle(new Point(5, 95), mySize.ToSize());

    StringFormat myFormat = new StringFormat(StringFormatFlags.FitBlackBox);

    e.Graphics.DrawString(myString, myFont, Brushes.Black, myRectangle, myFormat);

    e.Graphics.DrawRectangle(Pens.Black, myRectangle);

    The StringFormat was added after reading an example on line, doesn't seem to affect the output from what I can tell.

    Any suggestions?

    Thanks in advance,

    Linn

    • Moved by CoolDadTx Wednesday, July 10, 2013 1:57 PM Winforms related
    Tuesday, July 9, 2013 4:50 PM

Answers

All replies

  • Don't see the purpose of the MeasureString statement.  If you draw to a rectangle, the string will word wrap within the rectangle by default.
    Wednesday, July 10, 2013 8:27 AM
  • The amount of text I need to fit into the rectangle will vary from label to label.  I use the MeasureString to determine how large to make the rectangle.  Is this not the correct way to make this determination, is there a better way?

    From everything I've read I too expected the text to wrap by default but it isn't happening in this case.

    Looks like you are on to something though.  I hard coded the size of the rectangle, removing the reference to mySize, and the text does wrap.  But this won't work for the production version.  I do need to be able to adjust the size of that rectangle dynamically.

    Thanks,

    Linn

    Wednesday, July 10, 2013 1:11 PM
  • "I do need to be able to adjust the size of that rectangle dynamically."

    If the string won't fit the space available, it will be cut off.  For the text to wrap, there has to somewhere for it to wrap to.

    Wednesday, July 10, 2013 1:55 PM
  • If the string won't fit the space available, it will be cut off.  For the text to wrap, there has to somewhere for it to wrap to.

    Agreed, so my question is then, how do I determine the size of the rectangle based on the amount of text I wish to print?

    Thanks,

    Linn

    Wednesday, July 10, 2013 2:27 PM
  • "Agreed, so my question is then, how do I determine the size of the rectangle based on the amount of text I wish to print?"

    Use MeasureString.

    MeasureString returns the size of the rectangle required to hold the lines of text without wrapping. 

    Wednesday, July 10, 2013 2:41 PM
  • Ok, found a solution over on Stack Overflow...

    http://stackoverflow.com/questions/6752520/c-sharp-graphics-drawstring-rectanglef-auto-height-how-to-find-that-height

    Basically still using the MeasureString method but using it to just find the height of a new Size object to set the size of the rectangle.

    Seems to work fine.

    Thanks,

    Linn

    • Marked as answer by lkubler Wednesday, July 10, 2013 3:25 PM
    Wednesday, July 10, 2013 3:25 PM