1

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 is pulled from a database. The height of the each column of the table is fixed (currently), and I need to change it so if the string is a certain length, and therefore wraps, the second line of text is viewable (instead of hidden by the next row).

From my research, it seems like I can use MeasureString (since I know the font and string) to see if the string is longer/wider than my set table column, and change the height of the row if this is so. However, I'm very new to C# programming (and haven't done much programming overall in years, besides web stuff), so I'm not sure how to get all of this implemented. I have the logic in place, and I know how to change the height, I just need to know how to get an actual number I can use logic against using the MeasureString method (and how to instantiate any variables and functions I might need to use that method).

Nate
  • 30,286
  • 23
  • 113
  • 184
user1591298
  • 11
  • 1
  • 2

3 Answers3

2

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;
Nate
  • 30,286
  • 23
  • 113
  • 184
1

You can either use e.Graphics.MeasureString() or TextRenderer.MeasureText()

Differences and advantages of each of them are describe here:

TextRenderer.MeasureText and Graphics.MeasureString mismatch in size

There you will also find usage examples, which I would skip here to avoid duplication.

Community
  • 1
  • 1
George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
1

MSDN gives an example where you calculate this by registering an event handler to the OnPaint method of your control (in instantiated controls), or by overriding the OnPaint method (in inherited controls), or by overriding the OnPaint method of your form (not best practice since you probably don't want to do this for EVERY form repaint). The OnPaint method will give you access to a graphics object so you can call the MeasureString method.

Consider the following:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Paint += new PaintEventHandler(label1_Paint);
        }

        void label1_Paint(object sender, PaintEventArgs e)
        {
            SizeF size = e.Graphics.MeasureString(label1.Text, label1.Font);
            this.label1.Width = (int)size.Width;
            this.label1.Height = (int)size.Height;
        }
    }
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313