Paul Mehner

blog

  Home  |   Contact  |   Syndication    |   Login
  37 Posts | 0 Stories | 28 Comments | 15 Trackbacks

News

Archives

Post Categories

Old Blog

In older .net framework versions, calculating the height of a multi-line windows form label was not a straightforward task. I recently tripped acrossed a new technique available in the 2.0 framework for calculating the space that a label will occupy given its font and the runtime screen resolution, making this task easy.

using System.Windows.Forms
TextRenderer.MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags);

Sample Use:

Label lbC;
TextRenderer.MeasureText((IDeviceContext)lbC.CreateGraphics(), lbC.Text, lbC.Font, lbC.Size, TextFormatFlags.WordBreak);

 

posted on Friday, April 28, 2006 4:44 PM

Feedback

# re: Calculating Size of Multi-Line WinForm Label With MeasureText 6/25/2008 6:59 AM Jan Moeller
Thanks for TextRenderer sample
It works even better if you use lbC.ClientSize instead of lbC.Size, as the label uses a few pixels for border and margin.
- Jan


Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: