How to use MeasureString in C# to set table column width?

前端 未结 3 1994
花落未央
花落未央 2021-01-27 00:59

I have a pretty quick (and I\'m hoping basic) question. I\'m modifying some C# code for my company\'s website. The code draws a table for me in fixed columns, the data for which

3条回答
  •  无人及你
    2021-01-27 01:14

    I believe you need to use this overload for MeasureString(string,font,int):

    The width parameter specifies the maximum value of the width component of the returned SizeF structure (Width). If the width parameter is less than the actual width of the string, the returned Width component is truncated to a value representing the maximum number of characters that will fit within the specified width. To accommodate the entire string, the returned Height component is adjusted to a value that allows displaying the string with character wrap.

    -- From Above Linked MSDN Page (Emphasis mine)

    // Measure string (you'll need to instansiate your own graphics object, 
    // since you wont have PaintEventArgs)
    SizeF stringSize = new SizeF();
    stringSize = e.Graphics.MeasureString(measureString, stringFont, stringWidth);
    int cellHeight = stringSize.Height;
    

提交回复
热议问题